Document in Flutter PDF

10 Oct 20225 minutes to read

Adding the document settings

Flutter PDF supports various page setting options to control the page display, using the pageSettings property of PdfDocument.

You can choose the standard or custom page size when you add a page to the PDF document. The following sample explains how to create a PDF document with standard page size.

  • DART
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Set the page size
    document.pageSettings.size = PdfPageSize.a4;
    
    //Draw the text by adding page to the document
    document.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
        brush: PdfBrushes.mediumVioletRed,
        bounds: const Rect.fromLTWH(170, 100, 0, 0));
    
    //Save and dispose the PDF document
    File('Output.pdf').writeAsBytes(await document.save());
    document.dispose();

    You can create a PDF document with custom page size by using the following code snippet.

  • DART
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Set the page size
    document.pageSettings.size = const Size(200, 300);
    
    //Draw the text by adding page to the document
    document.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 19),
        brush: PdfBrushes.mediumVioletRed);
    
    //Save and close the PDF document
    File('Output.pdf').writeAsBytes(await document.save());
    document.dispose();

    You can change the page orientation from [portrait] to landscape using the PdfPageOrientation enum by the following code snippet.

  • DART
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Set the page size
    document.pageSettings.size = PdfPageSize.a4;
    
    //Change the page orientation to landscape
    document.pageSettings.orientation = PdfPageOrientation.landscape;
    
    //Draw the text by adding page to the document
    document.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
        brush: PdfBrushes.mediumVioletRed,
        bounds: const Rect.fromLTWH(170, 100, 0, 0));
    
    //Save and close the PDF document
    File('Output.pdf').writeAsBytes(await document.save());
    document.dispose();

    You can also change the orientation by setting the rotation angle using the PdfPageRotateAngle enum. The following code snippet explains the same.

  • DART
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Set the page size
    document.pageSettings.size = PdfPageSize.a4;
    
    //Change the page orientation to 90 degree
    document.pageSettings.rotate = PdfPageRotateAngle.rotateAngle90;
    
    //Draw the text by adding page to the document
    document.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
        brush: PdfBrushes.mediumVioletRed,
        bounds: const Rect.fromLTWH(170, 100, 0, 0));
    
    //Save and close the PDF document
    File('Output.pdf').writeAsBytes(await document.save());
    document.dispose();

    Creating sections in a PDF

    PDF sections are parts of a PDF document, which may contain one or more pages with their unique page settings. The following code snippet explains how to create a PdfSection in a PDF document.

  • DART
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Add a section to PDF document
    PdfSection section = document.sections!.add();
    
    //Draw the text by section page graphics
    section.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
        brush: PdfBrushes.mediumVioletRed,
        bounds: const Rect.fromLTWH(170, 100, 0, 0));
    
    //Save and close the PDF document
    File('Output.pdf').writeAsBytes(await document.save());
    document.dispose();

    Performing incremental update for the PDF document

    The Syncfusion Flutter PDF supports incremental update for the PDF document. The content of a PDF file can be updated incrementally without rewriting the entire file. The changes are appended to the end of the file, leaving its original contents intact. The main benefit is small changes to a large PDF document can be saved quickly but the resultant document size gets increased compared with the original PDF document. Disabling the incrementalUpdate of PdfFileStructure will rewrite the entire file, which results in a smaller PDF. This is explained in the following code sample.

  • DART
  • //Loads an existing PDF document
    PdfDocument document =
        PdfDocument(inputBytes: File('input.pdf').readAsBytesSync());
    
    //Disable the incremental update
    document.fileStructure.incrementalUpdate = false;
    
    //Set the compression level
    document.compressionLevel = PdfCompressionLevel.best;
    
    //Saves the document
    File('output.pdf').writeAsBytes(await document.save());
    
    //Disposes the document
    document.dispose();