flutter_html_to_pdf 0.3.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 81

flutter_html_to_pdf #

Flutter plugin for generating PDF files from HTML

Usage #

var htmlContent =
"""
<!DOCTYPE html>
<html>
<head>
  <style>
  table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
  }
  th, td, p {
    padding: 5px;
    text-align: left;
  }
  </style>
</head>
  <body>
    <h2>PDF Generated with flutter_html_to_pdf plugin</h2>
    <table style="width:100%">
      <caption>Sample HTML Table</caption>
      <tr>
        <th>Month</th>
        <th>Savings</th>
      </tr>
      <tr>
        <td>January</td>
        <td>100</td>
      </tr>
      <tr>
        <td>February</td>
        <td>50</td>
      </tr>
    </table>
    <p>Image loaded from web</p>
    <img src="https://i.imgur.com/wxaJsXF.png" alt="web-img">
  </body>
</html>
""";

var targetPath = "/your/sample/path";
var targetFileName = "example_pdf_file"

var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
    htmlContent, targetPath, targetFileName);

Code above simply generates PDF file from HTML content. It should work with most of common HTML markers. You don’t need to add .pdf extension to targetFileName because plugin only generates PDF files and extension will be added automatically.

Other Usages #

You can also pass File object with HTML content inside as parameter

var file = File("/sample_path/example.html");
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFile(
    file, targetPath, targetFileName);

or even just path to this file

var filePath = "/sample_path/example.html";
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFilePath(
    filePath, targetPath, targetFileName);

Images #

If your want to add local image from device to your HTML you need to pass path to image as src value.

<img src="file:///storage/example/your_sample_image.png" alt="web-img">

or if you want to use the image File object

<img src="${imageFile.path}" alt="web-img">

Remember that all files needs to be accessible from the application side. The plugin assumes that the user has granted all permissions! #

0.3.0 #

  • iOS: Fixed issue with loading local images to PDF document

0.2.3 #

  • Updated README.md

0.2.2 #

  • iOS: Changed string encoding to utf-8

0.2.1 #

  • Updated README
  • Updated CHANGELOG

0.2.0 #

  • Android: Changed location temporary generated pdf file
  • iOS: Added WKWebView cache clear
  • Updated example app

0.1.0 #

  • Initial release

example/README.md

example/main.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
import 'package:flutter_html_to_pdf/flutter_html_to_pdf.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

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

class _MyAppState extends State<MyApp> {
  String generatedPdfFilePath;

  @override
  void initState() {
    super.initState();
    generateExampleDocument();
  }

  Future<void> generateExampleDocument() async {
    var htmlContent = """
    <!DOCTYPE html>
    <html>
    <head>
      <style>
      table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
      }
      th, td, p {
        padding: 5px;
        text-align: left;
      }
      </style>
    </head>
      <body>
        <h2>PDF Generated with flutter_html_to_pdf plugin</h2>

        <table style="width:100%">
          <caption>Sample HTML Table</caption>
          <tr>
            <th>Month</th>
            <th>Savings</th>
          </tr>
          <tr>
            <td>January</td>
            <td>100</td>
          </tr>
          <tr>
            <td>February</td>
            <td>50</td>
          </tr>
        </table>

        <p>Image loaded from web</p>
        <img src="https://i.imgur.com/wxaJsXF.png" alt="web-img">
      </body>
    </html>
    """;

    Directory appDocDir = await getApplicationDocumentsDirectory();
    var targetPath = appDocDir.path;
    var targetFileName = "example-pdf";

    var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
        htmlContent, targetPath, targetFileName);
    generatedPdfFilePath = generatedPdfFile.path;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(),
      body: Center(
        child: RaisedButton(
          child: Text("Open Generated PDF Preview"),
          onPressed: () => Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => PDFViewerScaffold(
                        appBar: AppBar(title: Text("Generated PDF Document")),
                        path: generatedPdfFilePath)),
              ),
        ),
      ),
    ));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_html_to_pdf: ^0.3.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_html_to_pdf/flutter_html_to_pdf.dart';
  
Version Uploaded Documentation Archive
0.3.0 Jan 22, 2019 Go to the documentation of flutter_html_to_pdf 0.3.0 Download flutter_html_to_pdf 0.3.0 archive
0.2.3 Jan 21, 2019 Go to the documentation of flutter_html_to_pdf 0.2.3 Download flutter_html_to_pdf 0.2.3 archive
0.2.2 Jan 17, 2019 Go to the documentation of flutter_html_to_pdf 0.2.2 Download flutter_html_to_pdf 0.2.2 archive
0.2.1 Jan 17, 2019 Go to the documentation of flutter_html_to_pdf 0.2.1 Download flutter_html_to_pdf 0.2.1 archive
0.2.0 Jan 17, 2019 Go to the documentation of flutter_html_to_pdf 0.2.0 Download flutter_html_to_pdf 0.2.0 archive
0.1.0 Jan 16, 2019 Go to the documentation of flutter_html_to_pdf 0.1.0 Download flutter_html_to_pdf 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
67
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
89
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1
  • Flutter: 1.2.1

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/file_utils.dart. (-0.50 points)

Analysis of lib/file_utils.dart reported 1 hint:

line 4 col 10: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Format lib/flutter_html_to_pdf.dart.

Run flutter format to format lib/flutter_html_to_pdf.dart.

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Maintenance suggestions

The package description is too short. (-11 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8