Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter creat pdf file with different page size and font size

Tags:

pdf

size

flutter

Here with the code to create a pdf file for flutter. I am using plugin pdf 1.0.6.

My questions:

  • How to create a pdf file width 75mm with auto height to fit the content?
  • How to create multi lines of text with different font size?

    PDFDocument _generateDocument() {
    final pdf = new PDFDocument(deflate: zlib.encode);
    final page = new PDFPage(pdf, pageFormat: PDFPageFormat.A4);
    final g = page.getGraphics();
    final font = new PDFFont(pdf);
    final top = page.pageFormat.height;
    
    g.setColor(new PDFColor(0.0, 1.0, 1.0));
    g.drawRect(50.0 * PDFPageFormat.MM, top - 80.0 * PDFPageFormat.MM,
        100.0 * PDFPageFormat.MM, 50.0 * PDFPageFormat.MM);
    g.fillPath();
    
    g.setColor(new PDFColor(0.3, 0.3, 0.3));
    g.drawString(font, 12.0, "Hello World!", 10.0 * PDFPageFormat.MM,
        top - 10.0 * PDFPageFormat.MM);
    
    return pdf;
    

    }

like image 514
GPH Avatar asked Oct 16 '18 10:10

GPH


1 Answers

I recommend you to update to the latest version pdf: ^1.4. as a lot has changed on how to build a pdf.

  • For the format size I would try PdfPageFormat.undefined or in your case PdfPageFormat(75, double.infinity)

https://github.com/DavBfr/dart_pdf/blob/78232de1f962ea04c7e950d0f737417a35863071/pdf/lib/src/page_format.dart#L49

  • For the multiline with different size I would use the new way of creating a pdf that uses widgets. See the example:

https://github.com/DavBfr/dart_pdf/blob/master/pdf/example/main.dart

As simple as

pdf.addPage(Page(
    pageFormat: PdfPageFormat.a4,
    build: (Context context) {
      return Center(
        child: Text('Hello World', style: TextStyle(fontSize: 40)),
      ); // Center
    })); // Page
like image 65
jamesblasco Avatar answered Nov 13 '22 03:11

jamesblasco