images_to_pdf 0.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new73

images_to_pdf #

A new flutter plugin project.

Getting Started #

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

0.0.1 #

  • Initial version

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;
import 'package:flutter/services.dart' show rootBundle;
import 'package:images_to_pdf/images_to_pdf.dart';
import 'package:path_provider/path_provider.dart';
import 'package:printing/printing.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  File _pdfFile;
  String _status = "Not created";
  FileStat _pdfStat;
  bool _generating = false;

  Future<File> _assetFromBundle(String name) async {
    final tempDir = await getApplicationDocumentsDirectory();
    final output = File(path.join(tempDir.path, name));

    if (!await output.exists()) {
      final data = await rootBundle.load('assets/$name');
      final buffer = data.buffer;
      output.writeAsBytes(
          buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
    }

    return output;
  }

  Future<void> _createPdf() async {
    try {
      this.setState(() => _generating = true);
      final tempDir = await getApplicationDocumentsDirectory();
      final output = File(path.join(tempDir.path, 'example.pdf'));

      this.setState(() => _status = 'Preparing images...');
      final images = [
        await _assetFromBundle('01.png'),
        await _assetFromBundle('02.jpg'),
        await _assetFromBundle('03.jpg'),
        await _assetFromBundle('04.jpg'),
        await _assetFromBundle('05.jpg'),
        await _assetFromBundle('06.jpg'),
        await _assetFromBundle('07.jpg'),
        await _assetFromBundle('08.jpg'),
        await _assetFromBundle('09.jpg'),
        await _assetFromBundle('10.jpg'),
        await _assetFromBundle('11.jpg'),
        await _assetFromBundle('12.jpg'),
        await _assetFromBundle('13.jpg'),
      ];

      this.setState(() => _status = 'Generating PDF');
      await ImagesToPdf.createPdf(
        pages: images
            .map(
              (file) => PdfPage(
                imageFile: file,
                size: Size(1920, 1080),
                compressionQuality: 0.5,
              ),
            )
            .toList(),
        output: output,
      );
      _pdfStat = await output.stat();
      this.setState(() {
        _pdfFile = output;
        _status = 'PDF Generated (${_pdfStat.size ~/ 1024}kb)';
      });
    } catch (e) {
      this.setState(() => _status = 'Failed to generate pdf: $e".');
    } finally {
      this.setState(() => _generating = false);
    }
  }

  Future<void> _openPdf() async {
    if (_pdfFile != null) {
      try {
        final bytes = await _pdfFile.readAsBytes();
        await Printing.sharePdf(
            bytes: bytes, filename: path.basename(_pdfFile.path));
      } catch (e) {
        _status = 'Failed to open pdf: $e".';
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final isLoading = _generating;
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(_status),
        ),
        body: Column(
          children: <Widget>[
            if (isLoading) CircularProgressIndicator(),
            if (!isLoading) ...[
              if (_pdfFile == null)
                RaisedButton(
                  child: Text("Generate PDF"),
                  onPressed: _createPdf,
                ),
              if (_pdfFile != null)
                RaisedButton(
                  child: Text("Open PDF"),
                  onPressed: _openPdf,
                ),
            ]
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  images_to_pdf: ^0.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:images_to_pdf/images_to_pdf.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
49
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test