pdf method

Future<Uint8List?> pdf({
  1. PaperFormat? format,
  2. num? scale,
  3. bool? displayHeaderFooter,
  4. String? headerTemplate,
  5. String? footerTemplate,
  6. bool? printBackground,
  7. bool? landscape,
  8. String? pageRanges,
  9. bool? preferCssPageSize,
  10. PdfMargins? margins,
  11. IOSink? output,
})

Generates a pdf of the page with print css media. To generate a pdf with screen media, call Page.emulateMedia('screen') before calling page.pdf():

NOTE Generating a pdf is currently only supported in Chrome headless. NOTE By default, page.pdf() generates a pdf with modified colors for printing. Use the -webkit-print-color-adjust property to force rendering of exact colors.

// Generates a PDF with 'screen' media type.
await page.emulateMediaType(MediaType.screen);
await page.pdf(output: File('page.pdf').openWrite());

Parameters:

  • scale: Scale of the webpage rendering. Defaults to 1. Scale amount must be between 0.1 and 2.
  • displayHeaderFooter: Display header and footer. Defaults to false.
  • headerTemplate: HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
    • date formatted print date
    • title document title
    • url document location
    • pageNumber current page number
    • totalPages total pages in the document
  • footerTemplate: HTML template for the print footer. Should use the same format as the headerTemplate.
  • printBackground: Print background graphics. Defaults to false.
  • landscape: Paper orientation. Defaults to false.
  • pageRanges: Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
  • format: Paper format. Defaults to PageFormat.letter (8.5 inches x 11 inches).
  • margins: Paper margins, defaults to none.
  • preferCssPageSize: Give any CSS @page size declared in the page priority over what is declared in format. Defaults to false, which will scale the content to fit the paper size.
  • output an IOSink where to write the PDF bytes. This parameter is optional, if it is not provided, the bytes are returned as an in-memory list of bytes from the function.

If output parameter is null, this returns a Future<Uint8List> which resolves with PDF bytes. If output is not null, the method return null and the PDF bytes are written in the output sink.

NOTE headerTemplate and footerTemplate markup have the following limitations:

  1. Script tags inside templates are not evaluated.
  2. Page styles are not visible inside templates.

Implementation

Future<Uint8List?> pdf(
    {PaperFormat? format,
    num? scale,
    bool? displayHeaderFooter,
    String? headerTemplate,
    String? footerTemplate,
    bool? printBackground,
    bool? landscape,
    String? pageRanges,
    bool? preferCssPageSize,
    PdfMargins? margins,
    IOSink? output}) async {
  scale ??= 1;
  displayHeaderFooter ??= false;
  headerTemplate ??= '';
  footerTemplate ??= '';
  printBackground ??= false;
  landscape ??= false;
  pageRanges ??= '';
  preferCssPageSize ??= false;
  format ??= PaperFormat.letter;
  margins ??= PdfMargins.zero;

  var result = await devTools.page.printToPDF(
      transferMode: output == null ? 'ReturnAsBase64' : 'ReturnAsStream',
      landscape: landscape,
      displayHeaderFooter: displayHeaderFooter,
      headerTemplate: headerTemplate,
      footerTemplate: footerTemplate,
      printBackground: printBackground,
      scale: scale,
      paperWidth: format.width,
      paperHeight: format.height,
      marginTop: margins.top,
      marginBottom: margins.bottom,
      marginLeft: margins.left,
      marginRight: margins.right,
      pageRanges: pageRanges,
      preferCSSPageSize: preferCssPageSize);

  if (output == null) {
    return base64Decode(result.data);
  } else {
    await readStream(devTools.io, result.stream!, output);
    await output.close();
    return null;
  }
}