Skip to content

v0.1 Screenshots and PDF generation

Compare
Choose a tag to compare
@kblok kblok released this 01 Mar 11:48
· 1215 commits to master since this release
e7f4ff4

The first milestone on the roadmap is completed.

PDF support

You can export any page to PDF file.

var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(options, chromiumRevision);
var page = await browser.NewPageAsync();
await page.GoToAsync("https://www.google.com");
await page.PdfAsync(outputFile);

You can also get a PDF in a stream.

var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(options, chromiumRevision);
var page = await browser.NewPageAsync();
await page.GoToAsync("https://www.google.com");
var stream = await page.PdfStreamAsync();
await UploadToAzure(stream);

PdfAsync supports the following options:

  • Scale <decimal> Scale of the webpage rendering. Defaults to 1.
  • DisplayHeaderFooter <bool> Display header and footer. Defaults to false.
  • HeaderTemplate <string> HTML template for the print header.
  • FooterTemplate <string> HTML template for the print footer. Should use the same format as the HeaderTemplate.
  • PrintBackground <bool> Print background graphics. Defaults to false.
  • Landscape <boolean> Paper orientation. Defaults to false.
  • PageRanges <string> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
  • Format <string> Paper format. If set, takes priority over width or height options. Defaults to 'Letter'.
  • Width <string> Paper width, accepts values labeled with units.
  • Height <string> Paper height, accepts values labeled with units.
  • Margin <MarginOptions> Paper margins, defaults to none.
  • Top <string> Top margin, accepts values labeled with units.
  • Right <string> Right margin, accepts values labeled with units.
  • Bottom <string> Bottom margin accepts values labeled with units.
  • Left <string> Left margin, accepts values labeled with units.

Documentation adapted from the original Puppeteer repo.

Screenshot support

You can take a screenshot of a page (the visible part based on the viewport), a full page or a part of it.

var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(options, chromiumRevision);
var page = await browser.NewPageAsync();

await page.SetViewport(new ViewPortOptions
    {
        Width = 500,
        Height = 500
    });

await page.GoToAsync("https://www.google.com");
await page.ScreenshotAsync(outputFile);

You can also use ScreenshotStreamAsync to get a stream instead of a file.

The ScreenshotAsync method supports the following options:

  • Type <string> Specify screenshot type, can be either jpeg or png. Defaults to 'png'.
  • Quality <decimal?> The quality of the image, between 0-100. Not applicable to png images.
  • FullPage <bool> When true, takes a screenshot of the full scrollable page. Defaults to false.
  • Clip <Clip> An object which specifies clipping region of the page. Should have the following fields:
    • X <int> x-coordinate of top-left corner of clip area.
    • Y <int> y-coordinate of top-left corner of clip area.
    • Width <int> width of clipping area.
    • Height <number> height of clipping area.
  • OmitBackground <bool> Hides default white background and allows capturing screenshots with transparency. Defaults to false.

Documentation adapted from the original Puppeteer repo.