pdf method
Future<Uint8List?>
pdf({
- PaperFormat? format,
- num? scale,
- String? headerTemplate,
- bool? printBackground,
- bool? landscape,
- String? pageRanges,
- bool? preferCssPageSize,
- PdfMargins? margins,
- 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 to1
. Scale amount must be between 0.1 and 2.displayHeaderFooter
: Display header and footer. Defaults tofalse
.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 datetitle
document titleurl
document locationpageNumber
current page numbertotalPages
total pages in the document
footerTemplate
: HTML template for the print footer. Should use the same format as theheaderTemplate
.printBackground
: Print background graphics. Defaults tofalse
.landscape
: Paper orientation. Defaults tofalse
.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 toPageFormat.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 informat
. Defaults tofalse
, 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
andfooterTemplate
markup have the following limitations:
- Script tags inside templates are not evaluated.
- 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;
}
}