CWebView::SaveDocumentToMemory Method  
 
DWORD SaveDocumentToMemory(
  LPBYTE lpBuffer,  
  DWORD dwBufferSize,  
  LPWEBVIEW_PRINT_SETTINGS lpSettings  
);

The SaveDocumentToMemory method saves the current document to memory, with options to control page dimensions, margins, scaling, orientation, backgrounds, and headers and footers.

Parameters

lpBuffer
A pointer to a byte array which will contain the PDF when the method returns. If this parameter is not NULL, the buffer must be large enough to store the complete PDF or an error will be returned. If this parameter is NULL, the method will return the size of the PDF document in memory without copying any data.
dwBufferSize
An unsigned integer which specifies the maximum number of bytes which may be copied into the buffer. If the lpBuffer parameter is NULL, this value should be zero.
lpSettings
An optional pointer to a WEBVIEW_PRINT_SETTINGS structure which controls PDF formatting. Initialize the structure to zero, then set its dwSize member to sizeof(WEBVIEW_PRINT_SETTINGS) and specify the required settings. If this parameter is NULL or has been omitted, default PDF settings are used.

Return Value

If the method succeeds, the return value is the size of the PDF document in memory. If the method fails, the return value is zero. To get extended error information, call the GetLastError method.

Remarks

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.

Example

// Save the current document as a PDF in memory
DWORD dwDocumentSize = pWebView->SaveDocumentToMemory(NULL, 0);

if (cbDocumentSize > 0)
{
    _tprintf(_T("The current PDF document size is %lu bytes\n"), dwDocumentSize);

    LPBYTE lpDocument = (LPBYTE)malloc(dwDocumentSize);
    if (!lpDocument)
    {
        _tprintf(_T("Unable to allocate memory for the PDF document\n"));
        _exit(1);
    }

    DWORD dwCopied = pWebView->SaveDocumentToMemory(
                        lpDocument,
                        dwDocumentSize);

    if (dwCopied > 0)
    {
        // lpDocument contains the current document as a PDF
    }

    free(lpDocument);
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also