The SaveDocumentToMemory method generates a PDF
document directly in memory without creating an intermediate file on
disk. This can be useful for applications that need to transmit,
store, or process PDF documents without first saving them to a
temporary file. The maximum size of the document returned by this
method is approximately 4 GiB. Larger documents should be saved to
disk using the SaveDocument method.
If you wish to dynamically allocate memory for the PDF, call
this method twice. The first call should specify the lpBuffer
parameter as NULL and the dwBufferSize parameter as zero. The
method will cause the library to generate the PDF document, cache
the contents and return the size of the document in memory. Use that
value to allocate enough memory to store the complete document, and
call the method again with that value. If you need to provide print
settings, the first method call should include a pointer to a
WEBVIEW_PRINT_SETTINGS structure. The second call can
specify lpSettings as NULL, which tells the method to use
cached document with the previous printer settings.
If you are dynamically allocating memory for the PDF by calling
this method twice, those calls should be made immediately after one
another. If you allow a user to navigate away from the current page,
it will invalidate the document size returned by the first method
call.
If the lpSettings parameter is not NULL, it can be used to
control the page dimensions, margins, scaling, orientation, background
colors and images, and headers and footers. The flags in the structure's
dwFlags member determine which optional settings are applied:
WEBVIEW_PRINT_FLAG_PAGE_SIZE applies
dPageWidth and dPageHeight, in inches. Each
invalid dimension is replaced with the corresponding
library default.
WEBVIEW_PRINT_FLAG_MARGINS applies all
four margin members, in inches. Zero requests no margin;
negative or invalid values are converted to zero.
WEBVIEW_PRINT_FLAG_SCALE applies dScale.
A value of 1.0 represents 100 percent. Non-zero
values are limited to the range 0.1 through 2.0. Zero or
an invalid value leaves the default scale unchanged.
WEBVIEW_PRINT_FLAG_BACKGROUND includes
document background colors and images. Backgrounds are
omitted when this flag is not specified.
WEBVIEW_PRINT_FLAG_HEADERS includes both
a header and a footer. The lpszHeaderText and
lpszFooterText members can specify custom text.
Headers and footers are omitted when this flag is not
specified.
The nOrientation member selects portrait or landscape
orientation independently of dwFlags. Setting page
dimensions, margins, or scaling without the corresponding flag
has no effect. In particular, zero-initialized margins specify
no margins when WEBVIEW_PRINT_FLAG_MARGINS is enabled;
they do not request the default margins.
When headers and footers are enabled, a NULL
lpszHeaderText uses the document title, and a NULL
lpszFooterText uses the document URI. An empty string
suppresses the corresponding text without disabling the other
header or footer elements. Allow suitable top and bottom margins
for this content.
Printer-specific members are ignored when saving directly
to PDF. These members are nColorMode, nCopies,
nDuplex, nPagesPerSide, nCollation,
lpszPrinter, and lpszRanges. Consequently, these
settings cannot request grayscale PDF output, restrict the PDF
to a page range, or place multiple document pages on each PDF
page.
PDF output renders the complete document using the browser's
print layout, including content beyond the visible browser
area. The document's print styles can affect its appearance and
pagination. PDF output can therefore differ from the page as
displayed on the screen.
The method is synchronous and processes messages while the
browser completes the process of generating the PDF. It returns
after the PDF has been created in memory or the operation has
failed. If the browser is currently in the process of navigating
to a new location, this method will fail with the error
ST_ERROR_OPERATION_IN_PROGRESS. Although this
method does not create an intermediate file on disk, it cannot
guarantee that the document data will never be cached by the
WebView2 runtime.
This method requires Microsoft Edge WebView2 Runtime version
109.0.1518.0 or later. Earlier versions of the runtime do not
support the PrintToPdfStream method used to generate
PDF documents in memory. If the installed runtime does not support
this functionality, the method fails with the error code
ST_ERROR_OPERATION_NOT_SUPPORTED.
When the current document is saved to memory, this method
caches the result until the browser navigates to a new location
or the session is reset with a call to the Reset method.