Browser

Printing to PDF

PrintToPDF takes a GPALFile and writes a PDF to it. Every selector on the unit of work gets a PDF of its own, so one selector writes one file and three selectors write three, which is what it is mainly for: an embedded report, a statement, an invoice, a chart, the part of a page worth keeping without the navigation and the adverts around it. With no selector set it prints the whole page. Each file comes from GPALFile.Next, so the names a workflow supplied are used in order and anything past them is built from the last one, and the files that were written are in ReturnFilenames rather than Filename. The element's styling goes with it. GPAL collects the page's stylesheets and the element's computed style and sends them along with the markup, so the PDF looks like what was on screen rather than unstyled HTML. On OttoMagic the extension supplies both together through GetContentAndCss; on the other engines GPAL reads them from the page itself. WithPageOrientation sets portrait or landscape.

NOTE

This is not a picture of the page. The element and its styling are rendered to a PDF, so the text in it is text: selectable, searchable, and readable by anything that reads a PDF. That is the difference between printing a report and capturing an image of one, and it is why a statement or an invoice comes out of this rather than out of a screen capture.

WARNING

Like a click, PrintToPDF waits out the unit of work's WaitFor and then consumes every selector named before it. So .WithSelector(a).PrintToPDF(one).WithSelector(b).PrintToPDF(two) prints a and then b, not a and then both.

Examples

GPAL Fluent: High-level fluent C# API

//PrintToPDF takes a GPALFile, and since GPALFile converts from a string a plain path works too. The selector decides what is printed: the element it matched, or the whole page when none is set. WithPageOrientation is a setting and can be said anywhere before the print.

// the element that matters, without the rest of the page

Selector report = (Selector)GPAL.Selector

.WithSelectorName("Report")

.WithCSS("#statement");


GPAL.Browser

.GoTo("https://example.com/account")

.WithSelector(report)

.PrintToPDF(@"C:statementsmarch.pdf");


// no selector, so the whole page

GPAL.Browser

.GoTo("https://example.com/terms")

.PrintToPDF(@"C:statements erms.pdf");


// landscape, for something wider than it is tall

GPAL.Browser

.GoTo("https://example.com/dashboard")

.WithPageOrientation(PageOrientation.Landscape)

.WithSelector(report)

.PrintToPDF(@"C:statementsdashboard.pdf");

💬 Ask GPAL