Afur / flutter_html_to_pdf

Flutter plugin for generating PDF files from HTML
MIT License
69 stars 126 forks source link

Assets Images not rendering issue. How to render local asset-images inside html <img src...> tag in flutter? #18

Open jazzbpn opened 5 years ago

jazzbpn commented 5 years ago

Source Code:

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

import 'dart:async';
import 'dart:io';
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(),
  ));
}

var base64Image = "";

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="file:///storage/example/your_sample_image.png" alt="web-img">
          <img src="https://i.imgur.com/wxaJsXF.png" alt="web-img">
          <img src="file:///assets/profyl.jpg" alt="asset-img">
          <img src="assets/profyl.jpg" alt="asset-img">
          <img src="file:///storage/assets/profyl.png" alt="storage-img">
          <img src="file:///android_asset/flutter_assets/assets/profyl.jpg" alt="android_asset-img">
          <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
      AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
          9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Base64 Image" style="height: 90%;" />
        </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)),
          ),
        ),
      ),
    ));
  }
}

`

This code doesn't work!!

Screen Shot 2019-09-25 at 12 47 54 PM

Output

Screen Shot 2019-09-25 at 12 51 48 PM
talelamira commented 5 years ago

@jazzbpn I'm in the same issue and I wanted to thank you for the src="data:image/png;base64 tip. It worked for me and it's an acceptable workaround, enough to not drop the towel :)

johannphilippe commented 4 years ago

Does this "src=data:image/png;base64" tip means you pass the raw image data ? Does something like ByteData of loaded image would be ok ?

DirkBroenink commented 2 years ago

Running into the same issue. Has this been solved?