JavaScript in HTML To PDF

Any JavaScript code within the HTML will be executed during the rendering process, whether it's from an HTML string, file, or URL to a PDF document.

To enable JavaScript execution, set the enableJavaScript property to true. In some cases, such as when loading charts and graphs with JavaScript, you may also need to set a waitFor property. This ensures that there is sufficient time for the JavaScript code to execute properly.

For more detailed instructions on enabling JavaScript in IronPDF, visit the JavaScript Code Examples page.

Example of Enabling JavaScript in IronPDF

Here is a concise example of how you can enable JavaScript execution in IronPDF:

// Import the needed namespace
using IronPdf;

class Example 
{
    static void Main() 
    {
        // Create an instance of HtmlToPdf
        HtmlToPdf Renderer = new HtmlToPdf();

        // Enable JavaScript execution
        Renderer.PrintOptions.EnableJavaScript = true;

        // If necessary, set a wait time until JavaScript finishes execution
        // Wait 2 seconds for JavaScript to execute before rendering the page
        Renderer.PrintOptions.RenderDelay = 2000;

        // Render the PDF from a given HTML string or file
        // Placeholder HTML string or file path
        var PDF = Renderer.RenderHtmlAsPdf("<html><body><p>Hello, World!</p></body></html>");

        // Save the PDF document to a file
        PDF.SaveAs("output.pdf");
    }
}
// Import the needed namespace
using IronPdf;

class Example 
{
    static void Main() 
    {
        // Create an instance of HtmlToPdf
        HtmlToPdf Renderer = new HtmlToPdf();

        // Enable JavaScript execution
        Renderer.PrintOptions.EnableJavaScript = true;

        // If necessary, set a wait time until JavaScript finishes execution
        // Wait 2 seconds for JavaScript to execute before rendering the page
        Renderer.PrintOptions.RenderDelay = 2000;

        // Render the PDF from a given HTML string or file
        // Placeholder HTML string or file path
        var PDF = Renderer.RenderHtmlAsPdf("<html><body><p>Hello, World!</p></body></html>");

        // Save the PDF document to a file
        PDF.SaveAs("output.pdf");
    }
}
' Import the needed namespace
Imports IronPdf

Friend Class Example
	Shared Sub Main()
		' Create an instance of HtmlToPdf
		Dim Renderer As New HtmlToPdf()

		' Enable JavaScript execution
		Renderer.PrintOptions.EnableJavaScript = True

		' If necessary, set a wait time until JavaScript finishes execution
		' Wait 2 seconds for JavaScript to execute before rendering the page
		Renderer.PrintOptions.RenderDelay = 2000

		' Render the PDF from a given HTML string or file
		' Placeholder HTML string or file path
		Dim PDF = Renderer.RenderHtmlAsPdf("<html><body><p>Hello, World!</p></body></html>")

		' Save the PDF document to a file
		PDF.SaveAs("output.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel
  • EnableJavaScript: This ensures that JavaScript within your HTML content is executed, which is essential for rendering dynamic content such as charts and graphs.
  • RenderDelay: The RenderDelay option lets you specify a wait time (in milliseconds) to allow the JavaScript to fully execute before the page is rendered as a PDF. This is particularly useful when the JavaScript involves asynchronous operations or animations.

Additionally, check out the JavaScript Code Examples page for further examples and detailed information on handling different types of JavaScript within IronPDF.