printToPDF method

Future<PrintToPDFResult> printToPDF({
  1. bool? landscape,
  2. bool? displayHeaderFooter,
  3. bool? printBackground,
  4. num? scale,
  5. num? paperWidth,
  6. num? paperHeight,
  7. num? marginTop,
  8. num? marginBottom,
  9. num? marginLeft,
  10. num? marginRight,
  11. String? pageRanges,
  12. String? headerTemplate,
  13. String? footerTemplate,
  14. bool? preferCSSPageSize,
  15. @Enum(['ReturnAsBase64', 'ReturnAsStream']) String? transferMode,
  16. bool? generateTaggedPDF,
  17. bool? generateDocumentOutline,
})

Print page as PDF. landscape Paper orientation. Defaults to false. displayHeaderFooter Display header and footer. Defaults to false. printBackground Print background graphics. Defaults to false. scale Scale of the webpage rendering. Defaults to 1. paperWidth Paper width in inches. Defaults to 8.5 inches. paperHeight Paper height in inches. Defaults to 11 inches. marginTop Top margin in inches. Defaults to 1cm (~0.4 inches). marginBottom Bottom margin in inches. Defaults to 1cm (~0.4 inches). marginLeft Left margin in inches. Defaults to 1cm (~0.4 inches). marginRight Right margin in inches. Defaults to 1cm (~0.4 inches). pageRanges Paper ranges to print, one based, e.g., '1-5, 8, 11-13'. Pages are printed in the document order, not in the order specified, and no more than once. Defaults to empty string, which implies the entire document is printed. The page numbers are quietly capped to actual page count of the document, and ranges beyond the end of the document are ignored. If this results in no pages to print, an error is reported. It is an error to specify a range with start greater than end. 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

For example, <span class=title></span> would generate span containing the title. footerTemplate HTML template for the print footer. Should use the same format as the headerTemplate. preferCSSPageSize Whether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size. transferMode return as stream generateTaggedPDF Whether or not to generate tagged (accessible) PDF. Defaults to embedder choice. generateDocumentOutline Whether or not to embed the document outline into the PDF.

Implementation

Future<PrintToPDFResult> printToPDF(
    {bool? landscape,
    bool? displayHeaderFooter,
    bool? printBackground,
    num? scale,
    num? paperWidth,
    num? paperHeight,
    num? marginTop,
    num? marginBottom,
    num? marginLeft,
    num? marginRight,
    String? pageRanges,
    String? headerTemplate,
    String? footerTemplate,
    bool? preferCSSPageSize,
    @Enum(['ReturnAsBase64', 'ReturnAsStream']) String? transferMode,
    bool? generateTaggedPDF,
    bool? generateDocumentOutline}) async {
  assert(transferMode == null ||
      const ['ReturnAsBase64', 'ReturnAsStream'].contains(transferMode));
  var result = await _client.send('Page.printToPDF', {
    if (landscape != null) 'landscape': landscape,
    if (displayHeaderFooter != null)
      'displayHeaderFooter': displayHeaderFooter,
    if (printBackground != null) 'printBackground': printBackground,
    if (scale != null) 'scale': scale,
    if (paperWidth != null) 'paperWidth': paperWidth,
    if (paperHeight != null) 'paperHeight': paperHeight,
    if (marginTop != null) 'marginTop': marginTop,
    if (marginBottom != null) 'marginBottom': marginBottom,
    if (marginLeft != null) 'marginLeft': marginLeft,
    if (marginRight != null) 'marginRight': marginRight,
    if (pageRanges != null) 'pageRanges': pageRanges,
    if (headerTemplate != null) 'headerTemplate': headerTemplate,
    if (footerTemplate != null) 'footerTemplate': footerTemplate,
    if (preferCSSPageSize != null) 'preferCSSPageSize': preferCSSPageSize,
    if (transferMode != null) 'transferMode': transferMode,
    if (generateTaggedPDF != null) 'generateTaggedPDF': generateTaggedPDF,
    if (generateDocumentOutline != null)
      'generateDocumentOutline': generateDocumentOutline,
  });
  return PrintToPDFResult.fromJson(result);
}