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

not working on web #19

Open
Fevzettin1 opened this issue Dec 8, 2023 · 6 comments
Open

not working on web #19

Fevzettin1 opened this issue Dec 8, 2023 · 6 comments

Comments

@Fevzettin1
Copy link

I want to convert html to pdf in my flutter web project, but unfortunately the work in the example does not yield results.

Can you help with this?

@pramodkr123
Copy link

Are you getting any solution for the flutter web. I am unable to generate html to pdf in web.

@MGARockAByte
Copy link

I think it works on Flutter Web now. I'm running it with the current version. Have you tested it yet, @pramodkr123?

@pramodkr123
Copy link

Yes, I have tested it, but it doesn't work. Can you provide me with the code for the web version? @MGARockAByte

@MGARockAByte
Copy link

Yes, I have tested it, but it doesn't work. Can you provide me with the code for the web version? @MGARockAByte

Sure
main.dart:

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:htmltopdfwidgets/htmltopdfwidgets.dart' as html_to_pdf;

void main() async {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'HTML to PDF Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  final String htmlString = '''
    <h1>Hello world</h1>

    <p>This is a paragraph with a word in <b>bold</b> followed by a break.<br>The text comes after the break.</p>

    <p>A list:</p>
    <ul>
        <li>Element 1</li>
        <li>Element 2</li>
        <li>Element 3</li>
    </ul>

    <p>follow <a href="https://www.example.com">link</a></p>
    ''';

  void _downloadPdf() async {
    final pdfData = await _generatePdf();

    await Printing.sharePdf(
      bytes: pdfData,
      filename: 'example.pdf',
    );
  }

  Future<Uint8List> _generatePdf() async {
    final pdf = pw.Document();
    var htmlWidgets = await html_to_pdf.HTMLToPdf().convert(htmlString);
    pdf.addPage(
      pw.MultiPage(
        build: (pw.Context context) => [
          pw.Column(children: htmlWidgets),
        ],
      ),
    );

    return pdf.save();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: _downloadPdf,
        child: const Text('Generate and download PDF'),
      ),
    );
  }
}

pubspec.yaml:

name: html_to_pdf_tester
description: "A new Flutter project."

# Prevent accidental publishing to pub.dev.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ^3.5.4

dependencies:
  flutter:
    sdk: flutter
  htmltopdfwidgets: ^1.0.5
  pdf: ^3.11.1
  printing: ^5.13.4

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^4.0.0

flutter:
  uses-material-design: true

@pramodkr123
Copy link

pramodkr123 commented Dec 12, 2024

Thanks for the reply. My html is converting to pdf but i am getting one issue the table tag is not converting to pdf.
it is not converting to pdf same as html.

@alihassan143
Copy link
Owner

some properties of table are not supported yet I will work on once I am free currently I am busy with other projects

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

4 participants