Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingPluginException (No implementation found for method convertHtmlToPdf on channel flutter_html_to_pdf) #71

Open
xavierslee opened this issue Apr 14, 2023 · 0 comments

Comments

@xavierslee
Copy link

xavierslee commented Apr 14, 2023

Similar to #46,

I want to display the converted HTML in my pdf.addPage

I know there was a solution in the 'Specify iOS' #43, but it didnt work for me.

Im using printing package to preview it, but its not the package issue.

I made a new flutter project and tried but didnt work.

I tried other plugins but this package suits me best.

Any ideas? Do check my pubspec.yaml , my conversion and preview.

Those are the points I double checked

Future<Uint8List> generatePdf() async {
  final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
   Directory appDocDir = await getApplicationDocumentsDirectory();
   final targetPath = appDocDir.path;    
   var targetFileName = "receipt-example";
   var generatedPdfFile;

   try {
    generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
     htmlContent, targetPath, targetFileName);
   } on Exception catch (e) {
   print('Exception caught: $e');
   print('Object Response is: ' + generatedPdfFile.toString());
   }
  

   pdf.addPage(
   pw.Page(
     pageFormat: receiptFormat,
     build: (context) {
       return pw.Column(
         children: [
           pw.SizedBox( 
             width: double.infinity,
             child: pw.FittedBox(
               child: pw.Text('Receipt'),
             ),
           ),
           pw.SizedBox(height: 10),
           pw.Flexible(child: pw.FlutterLogo()), 
           pw.SizedBox(
             width: double.infinity,
            child: pw.Image(pw.MemoryImage(generatedPdfFile)),
           ),
         ],
       );
     },
   ),
 );
   return pdf.save();
 }
 

 @override
  Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text('PDF Preview Example'),
     ),
     body: Center(
       child: ElevatedButton(
         child: Text('Open Generated PDF Preview'),
         onPressed: () async {
           // Show the PDF preview
           await Navigator.of(context).push(
             MaterialPageRoute(
               builder: (_) => PdfPreview(
                build:(format) => generatePdf(),
               ),
             ),
           );
         },
       ),
     ),
   );
 }
}

This is the response from the try/catch

flutter: Exception caught: MissingPluginException(No implementation found for method convertHtmlToPdf on channel flutter_html_to_pdf)
flutter: Object Response is: null


The entire code:

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

import 'package:flutter/material.dart';
import 'package:flutter_html_to_pdf/flutter_html_to_pdf.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';



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

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

class _MyAppState extends State<MyApp> {
    final PdfPageFormat receiptFormat = PdfPageFormat(80.0, 200.0); //receipt dimentsions

  var generatedPdfFilePath;

   final 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>
    """;

  @override
  void initState() {
    super.initState();
  }
 
  Future<Uint8List> generatePdf() async {
 // Generate the PDF content
   final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
    Directory appDocDir = await getApplicationDocumentsDirectory();
    final targetPath = appDocDir.path;    
    var targetFileName = "receipt-example";
    var generatedPdfFile;
    try {
     generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
      htmlContent, targetPath, targetFileName);
    } on Exception catch (e) {
    print('Exception caught: $e');
    print('Object Response is: ' + generatedPdfFile.toString());
      // TODO
    }
   

    pdf.addPage(
    pw.Page(
      pageFormat: receiptFormat,
      build: (context) {
        return pw.Column(
          children: [
            pw.SizedBox( 
              width: double.infinity,
              child: pw.FittedBox(
                child: pw.Text('Receipt'),
              ),
            ),
            pw.SizedBox(height: 10),
            pw.Flexible(child: pw.FlutterLogo()), //Add QR Code
            pw.SizedBox(
              width: double.infinity,
              //child: pw.Html(htmlContent),
             //child: html.Html(data: htmlContent),
             child: pw.Image(pw.MemoryImage(generatedPdfFile)),
            ),
          ],
        );
      },
    ),
  );
    return pdf.save();
  }
  

  @override
   Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('PDF Preview Example'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Open Generated PDF Preview'),
          onPressed: () async {
            // Generate the PDF document
            //final pdfBytes = await gener();

            // Show the PDF preview
            await Navigator.of(context).push(
              MaterialPageRoute(
                builder: (_) => PdfPreview(
                 build:(format) => generatePdf(),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

Other exceptions- Caught by printing package (if this helps)


════════ Exception caught by printing ══════════════════════════════════════════
The following _TypeError was thrown while generating a PDF:
type 'Null' is not a subtype of type 'Uint8List'

When the exception was thrown, this was the stack
#0      _MyAppState.generatePdf.<anonymous closure>
#1      Page.postProcess
#2      Document.save
#3      _MyAppState.generatePdf
<asynchronous suspension>
#4      PdfPreviewRaster._raster
<asynchronous suspension>
PageFormat: "PdfPageFormat 595.275590551181x841.8897637795275 margins:56.69291338582677, 56.69291338582677, 56.69291338582677, 56.69291338582677"
════════════════════════════════════════════════════════════════════════════════

My pubspec.yaml

name: testing_packages
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev


version: 1.0.0+1

environment:
  sdk: '>=2.19.6 <3.0.0'


dependencies:
  flutter:
    sdk: flutter


  cupertino_icons: ^1.0.2
  path_provider: ^2.0.14
  flutter_html_to_pdf: ^0.7.0
  flutter_full_pdf_viewer: ^1.0.6
  printing: ^5.10.3
  pdf: ^3.10.1

dev_dependencies:
  flutter_test:
    sdk: flutter


  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true

plugin:
    platforms:
      android:
        package: com.afur.flutterhtmltopdf
        pluginClass: FlutterHtmlToPdfPlugin
      ios:
        pluginClass: FlutterHtmlToPdfPlugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant