SaveDocument Method  
 

Saves the currently displayed document in a specified format such as HTML, PDF, MHTML, or image.

Syntax

object.SaveDocument( FileName, [FileType] )

Parameters

FileName
A string which specifies the name of the local file. If the path includes environment variables enclosed in percent (%) symbols, the variables will be expanded. This parameter must specify a valid file path. The file name cannot specify a device, folder name or system file. If the file is marked as read-only, the method will fail.
FileType
An optional parameter which specifies the file format. If this parameter is omitted, the format will be based on the file name extension.
Value Value Description
webSaveAsDefault 0 Save using a format inferred from the file extension. If the file extension is not recognized, the contents will be saved as plain text. For example, if you specify a file name with the .html extension, it will be saved in HTML format. If the file name has a .pdf extension, it will be saved as a PDF.
webSaveAsText 1 Saves the current document as plain text. The HTML tags are removed and entities are converted to their UTF-8 equivalent. Extraneous whitespace within the text will be removed, but the overall structure of ordered and unordered lists will be preserved. Choose this format if you wish to save only the text content of a document.
webSaveAsHtml 2 Saves the current document as a UTF-8 encoded HTML file. This includes the complete source for the page, but does not include any of the additional assets such as images. Choose this format if you wish to save the document as an editable HTML file.
webSaveAsJson 3 Saves the current document as a JSON formatted string. The string will be quoted and reserved characters will be escaped. Choose this format if you wish to preserve the HTML document as a JSON string for logging or embedding in a JSON object.
webSaveAsMhtml 4 Saves the current document as a single-file web archive (MHTML) that inlines the HTML content and all of the resources it references. This format calls the DevTools Page.captureSnapshot method in the browser and writes the returned data to disk. MHTML is useful for offline archiving since it combines images, styles and scripts together in a single file.
webSaveAsPdf 5 Saves the current document as a PDF file which produces a paginated document suitable for printing and distribution. This is functionally similar to using the browser to print to a PDF, but does not display a dialog to the user.
webSaveAsJpeg 101  Save the current document view as a JPEG image. JPEG is a widely used image format optimized for photographs and complex images with smooth gradients. It uses lossy compression to significantly reduce file size while maintaining acceptable visual quality.
webSaveAsPng 102 Save the current document view as a PNG image. PNG is a lossless image format designed for crisp detail and high fidelity, especially in graphics with sharp edges, text, or flat colors. Although PNG files are typically larger than JPEGs, they preserve exact image data without quality loss.
webSaveAsWebP 103 Save the current document view as a WebP image. WebP is a modern image format developed by Google that supports both lossy and lossless compression, as well as transparency and animation. It delivers smaller file sizes compared to JPEG and PNG while maintaining comparable or better visual quality.

Return Value

Returns True if the method succeeds or False if the method fails.

Remarks

The SaveDocument method saves the current page to a local file in the format specified by the FileType parameter. The operation is synchronous, processing messages while the browser completes the action, and only returns once the document has been created or the operation has failed. A temporary file is created during the save process and then moved into place, replacing any existing file. This ensures that an existing file will not be changed unless the browser indicates the save operation was successful. Saving is not permitted while a navigation is in progress, and attempts to do so will fail with an error.

The file name specified must be valid, and environment variables in the path are expanded before use. When no file type is specified, the format is inferred from the file extension. For example, .html or .htm will save the document as HTML, .pdf will create a PDF file and .png will save the page as a PNG image. If no recognized extension is present, the page will be saved as plain text by default.

When saving the current document as plain text, the HTML markup will be removed and it will be saved as a UTF-8 encoded text file. When saving as HTML, the method saves the complete HTML source for the page. The JSON format also saves the HTML markup, but encodes it as a quoted JSON string with reserved characters escaped. Note that all text-related formats are saved using UTF-8 multi-byte encoding without a byte order mark (BOM). The text will not be automatically converted to the current user's locale. Most modern text editors automatically recognize UTF-8 encoding, but some legacy applications may assume the text is localized to the current code page.

The MHTML option creates a single-file archive which contains the page content and all of its resources such as images, style sheets, and scripts. This format is often used for offline archiving or sharing since everything is contained in one file. Saving as PDF renders the page into a paginated print-style format suitable for distribution. These formats all operate on the complete document, including content that extends beyond the visible area.

By contrast, saving as an image (JPEG, PNG, or WebP) captures only the visible viewport of the page at the time the method is called. Content that requires scrolling will not be included in the saved image. PNG is lossless and preserves transparency, while JPEG and WebP use compression to produce smaller files. WEBP generally offers better quality at a reduced size compared to JPEG, but support for the format may vary between applications. If you want a pixel-perfect representation of the current page as it appears on the screen, particularly if it contains a lot of text, it is recommended that you use the PNG format.

Because this method blocks the current thread until the save completes, it may take a noticeable amount of time to return if the current document is very large. If the method is called from a worker thread, the request is marshaled to the UI thread that manages the browser control. For applications with a graphical interface, it is recommended that you display the standard hourglass (or other busy indicator) while the save is in progress to provide a visual cue to the user.

If you want a copy of the HTML or plain text contents of the current page without saving it to a file, use the DocumentText or FilteredText properties.

See Also

DocumentText Property, FilteredText Property, PrintDocument Method