Interface IPdfGeneratorFinal
- Namespace
- CobaltPdf
- Assembly
- CobaltPdf.dll
Continuation of the fluent chain after WithCustomJS has been called.
Prevents adding a second custom JS block by accident.
public interface IPdfGeneratorFinal
Methods
AddCookie(string, string, string?, string)
Adds a browser cookie to the request context.
IPdfGeneratorFinal AddCookie(string name, string value, string? domain = null, string path = "/")
Parameters
Returns
AddLocalStorage(string, string)
Adds a key-value pair to the browser's Local Storage before rendering.
IPdfGeneratorFinal AddLocalStorage(string key, string value)
Parameters
Returns
AddSessionStorage(string, string)
Adds a key-value pair to the browser's Session Storage before rendering.
IPdfGeneratorFinal AddSessionStorage(string key, string value)
Parameters
Returns
RenderHtmlAsPdfAsync(string, CancellationToken)
Executes the generation process by rendering the provided HTML string.
Task<PdfDocument> RenderHtmlAsPdfAsync(string html, CancellationToken cancellationToken = default)
Parameters
htmlstringThe raw HTML content to render as a PDF.
cancellationTokenCancellationTokenOptional token to cancel the render.
Returns
- Task<PdfDocument>
A PdfDocument containing the generated binary data.
RenderHtmlFileAsPdfAsync(string, CancellationToken)
Reads an HTML file from disk and renders it as a PDF. Any fluent options set before this call are consumed and reset automatically.
Task<PdfDocument> RenderHtmlFileAsPdfAsync(string filePath, CancellationToken cancellationToken = default)
Parameters
filePathstringAbsolute or relative path to a
.htmlfile on disk.cancellationTokenCancellationTokenOptional token to cancel the render.
Returns
- Task<PdfDocument>
A PdfDocument containing the generated PDF bytes.
Exceptions
- FileNotFoundException
Thrown when
filePathdoes not exist.
RenderUrlAsPdfAsync(string, CancellationToken)
Executes the generation process by navigating to the specified URL.
Task<PdfDocument> RenderUrlAsPdfAsync(string url, CancellationToken cancellationToken = default)
Parameters
urlstringThe fully qualified web address to render.
cancellationTokenCancellationTokenOptional token to cancel the render.
Returns
- Task<PdfDocument>
A PdfDocument containing the generated binary data.
WithCspBypass(bool)
Enables or disables the bypass of the page's Content Security Policy (CSP).
IPdfGeneratorFinal WithCspBypass(bool enable = true)
Parameters
enablebool
Returns
WithCustomJS(string)
Custom JS has already been set. Combine all scripts into a single block.
[Obsolete("CobaltPdf: WithCustomJS has already been called. Combine your scripts into one block.", true)]
IPdfGeneratorFinal WithCustomJS(string script)
Parameters
scriptstring
Returns
WithEncryption(PdfEncryptionOptions)
Password-protects the output PDF.
IPdfGeneratorFinal WithEncryption(PdfEncryptionOptions encryption)
Parameters
encryptionPdfEncryptionOptions
Returns
WithFonts(string)
Configures a local directory to search for custom fonts during rendering.
IPdfGeneratorFinal WithFonts(string fontDirectoryPath)
Parameters
fontDirectoryPathstring
Returns
WithFooter(string)
Sets the HTML template or file path for the PDF footer.
IPdfGeneratorFinal WithFooter(string htmlOrFilePath)
Parameters
htmlOrFilePathstring
Returns
WithGrayscale()
Enables grayscale rendering for the PDF.
IPdfGeneratorFinal WithGrayscale()
Returns
WithHeader(string)
Sets the HTML template or file path for the PDF header.
IPdfGeneratorFinal WithHeader(string htmlOrFilePath)
Parameters
htmlOrFilePathstring
Returns
WithLandscape()
Sets the PDF orientation to landscape.
IPdfGeneratorFinal WithLandscape()
Returns
WithLazyLoadPages(int, TimeSpan?, TimeSpan?)
Scrolls through a specific number of pages to trigger lazy-loading.
IPdfGeneratorFinal WithLazyLoadPages(int pageCount, TimeSpan? delay = null, TimeSpan? timeout = null)
Parameters
pageCountintNumber of viewports to scroll.
delayTimeSpan?Time to wait at each step. Default is 200ms.
timeoutTimeSpan?Maximum time to wait for the entire scroll process. Default is 30s.
Returns
WithMargins(MarginOptions)
Sets the page margins for the rendered PDF. Defaults to 1 cm on all sides if not called.
IPdfGeneratorFinal WithMargins(MarginOptions margins)
Parameters
marginsMarginOptions
Returns
WithMediaType(CssMediaType)
Sets the CSS media type to emulate during rendering (e.g., Screen or Print).
IPdfGeneratorFinal WithMediaType(CssMediaType type)
Parameters
typeCssMediaType
Returns
WithMetadata(Action<MetadataOptions>)
Sets descriptive metadata (title, author, subject, etc.) embedded in the PDF document.
IPdfGeneratorFinal WithMetadata(Action<MetadataOptions> configure)
Parameters
configureAction<MetadataOptions>
Returns
WithPageRanges(string)
Restricts the output PDF to the specified page ranges.
IPdfGeneratorFinal WithPageRanges(string ranges)
Parameters
rangesstringComma-separated page ranges, e.g.
"1-3","1,4,7".
Returns
WithPageSize(string, string)
Sets custom page dimensions, overriding the paper format.
IPdfGeneratorFinal WithPageSize(string width, string height)
Parameters
widthstringCSS width string, e.g.
"8.5in","200mm".heightstringCSS height string, e.g.
"11in","297mm".
Returns
WithPaperFormat(string)
Sets the paper format (size) for the rendered PDF.
Defaults to "A4" if not called.
IPdfGeneratorFinal WithPaperFormat(string format)
Parameters
formatstringA Playwright paper format string, e.g.
"A4","A3","Letter","Legal","Tabloid".
Returns
WithPrintBackground(bool)
Enables or disables printing of CSS background graphics (colors, images).
IPdfGeneratorFinal WithPrintBackground(bool enable = true)
Parameters
enablebooltrueto print backgrounds (default);falseto suppress them.
Returns
WithReloadAfterStorage()
Requests a page reload after localStorage and sessionStorage values have been injected on the initial page load.
IPdfGeneratorFinal WithReloadAfterStorage()
Returns
WithScale(float)
Sets the scale factor for PDF content (0.1 – 2.0). Values below 1.0 shrink the content.
IPdfGeneratorFinal WithScale(float scale)
Parameters
scalefloatA value between
0.1and2.0.
Returns
WithWaitStrategy(WaitOptions)
Configures the strategy used to determine when the page is ready for PDF rendering. Use this to handle asynchronous content by waiting for a fixed duration or a specific event.
IPdfGeneratorFinal WithWaitStrategy(WaitOptions strategy)
Parameters
strategyWaitOptions
Returns
Remarks
Note: This is an assignment; if called multiple times, the last call in the chain wins.
Supported Strategies:
- Network IdleThe default. Waits until there are no network requests for at least 500ms.
- Fixed DelayWaits for a specific TimeSpan (e.g.,
WaitOptions.ForDelay(TimeSpan.FromSeconds(5))). - CSS/JS PollingWaits for a CSS selector to appear or a JS expression to return true.
- Manual Signal
The most robust method for complex apps. Use
WaitOptions.ForSignal(timeout)and callwindow.cobaltNotifyRender()inside your WithCustomJS(string) script to manually trigger the render once your logic is complete.
WithWatermark(WatermarkOptions)
Applies a watermark overlay to every page of the output PDF.
IPdfGeneratorFinal WithWatermark(WatermarkOptions watermark)
Parameters
watermarkWatermarkOptions