IronPDF How-Tos JavaScript (Custom Render Delays) How to use JavaScript with HTML to PDF ByChaknith Bin May 31, 2025 Updated June 22, 2025 Share: JavaScript is a high-level, versatile programming language commonly used in web development. It allows developers to add interactive and dynamic features to websites. jQuery, on the other hand, is not a separate programming language but rather a JavaScript library designed to simplify common tasks in JavaScript, such as DOM manipulation, event handling, and AJAX requests.IronPDF supports JavaScript efficiently via the Chromium rendering engine. In this article, we'll demonstrate how to use JavaScript and jQuery in HTML to PDF conversion in .NET C# projects. IronPDF provides a free trial of IronPDF for exploration. View the IronPDF YouTube Playlist Get started with IronPDF Start using IronPDF in your project today with a free trial. First Step: Start for Free How to Use JavaScript with HTML to PDF Download the C# library: Convert HTML to PDF Enable JavaScript execution: Use the EnableJavaScript property. Render from HTML to PDF: Ensure your JavaScript code is fully supported. Execute custom JavaScript: Within your .NET C# code. Capture console messages: Using a JavaScript message listener. Render JavaScript Example JavaScript functionalities in HTML are supported when converting from HTML to PDF. As rendering happens without delay, JavaScript might not be executed properly. In most cases, it is advised to configure the WaitFor class in rendering options to allow JavaScript time to execute properly. In the following code, WaitFor.JavaScript is used to specify a maximum wait time of 500 ms. :path=/static-assets/pdf/content-code-examples/how-to/javascript-to-pdf-render-javascript.cs using IronPdf; // This code demonstrates how to render HTML content with JavaScript into a PDF using IronPdf // HTML content that includes JavaScript string htmlWithJavaScript = @"<h1>This is HTML</h1> <script> document.write('<h1>This is JavaScript</h1>'); window.ironpdf.notifyRender(); </script>"; // Instantiate a ChromePdfRenderer to handle the rendering of the PDF var renderer = new ChromePdfRenderer(); // Enable the execution of JavaScript within the HTML content renderer.RenderingOptions.EnableJavaScript = true; // Set the JavaScript timeout to wait for JS execution before rendering the PDF // This is set in milliseconds (500 ms in this case) renderer.RenderingOptions.JavaScriptTimeout = 500; // Render the HTML (including JavaScript) to a PDF document var pdfJavaScript = renderer.RenderHtmlAsPdf(htmlWithJavaScript); // Save the rendered PDF document to a file named "javascriptHtml.pdf" pdfJavaScript.SaveAs("javascriptHtml.pdf"); Imports IronPdf ' This code demonstrates how to render HTML content with JavaScript into a PDF using IronPdf ' HTML content that includes JavaScript Private htmlWithJavaScript As String = "<h1>This is HTML</h1> <script> document.write('<h1>This is JavaScript</h1>'); window.ironpdf.notifyRender(); </script>" ' Instantiate a ChromePdfRenderer to handle the rendering of the PDF Private renderer = New ChromePdfRenderer() ' Enable the execution of JavaScript within the HTML content renderer.RenderingOptions.EnableJavaScript = True ' Set the JavaScript timeout to wait for JS execution before rendering the PDF ' This is set in milliseconds (500 ms in this case) renderer.RenderingOptions.JavaScriptTimeout = 500 ' Render the HTML (including JavaScript) to a PDF document Dim pdfJavaScript = renderer.RenderHtmlAsPdf(htmlWithJavaScript) ' Save the rendered PDF document to a file named "javascriptHtml.pdf" pdfJavaScript.SaveAs("javascriptHtml.pdf") $vbLabelText $csharpLabel Some very complex JavaScript frameworks might not always work 100% with IronPDF and .NET due to limited allocation of memory for JavaScript execution. Execute Custom JavaScript Example The JavaScript property in rendering options is used to execute custom JavaScript before rendering HTML to PDF. This is very useful when rendering from a URL where it is not possible to inject JavaScript code. Simply assign a JavaScript code to the JavaScript property of the rendering options. :path=/static-assets/pdf/content-code-examples/how-to/javascript-to-pdf-execute-javascript.cs using IronPdf; // Creates a new instance of the ChromePdfRenderer, // which allows for rendering HTML to PDF using Google Chrome's rendering engine. ChromePdfRenderer renderer = new ChromePdfRenderer(); // Enables JavaScript execution within the PDF rendering process. // This JavaScript code will locate all <h1> elements in the HTML and change // their text color to red. renderer.RenderingOptions.Javascript = @" document.querySelectorAll('h1').forEach(function(el){ el.style.color='red'; });"; // Renders the specified HTML content as a PDF. // The HTML contains an <h1> element with the text "Happy New Year!" which will have its color // changed to red by the JavaScript code. PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Happy New Year!</h1>"); // Saves the generated PDF document to a file named "executed_js.pdf". // This file will be created in the same directory where the application is running, // unless otherwise specified by a different path. pdf.SaveAs("executed_js.pdf"); Imports IronPdf ' Creates a new instance of the ChromePdfRenderer, ' which allows for rendering HTML to PDF using Google Chrome's rendering engine. Private renderer As New ChromePdfRenderer() ' Enables JavaScript execution within the PDF rendering process. ' This JavaScript code will locate all <h1> elements in the HTML and change ' their text color to red. renderer.RenderingOptions.Javascript = " document.querySelectorAll('h1').forEach(function(el){ el.style.color='red'; });" ' Renders the specified HTML content as a PDF. ' The HTML contains an <h1> element with the text "Happy New Year!" which will have its color ' changed to red by the JavaScript code. Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Happy New Year!</h1>") ' Saves the generated PDF document to a file named "executed_js.pdf". ' This file will be created in the same directory where the application is running, ' unless otherwise specified by a different path. pdf.SaveAs("executed_js.pdf") $vbLabelText $csharpLabel JavaScript Message Listener Example IronPDF allows you to listen to the JavaScript messages that have been logged to the console. Both error logs and custom log messages will be captured by the library. Utilize the JavascriptMessageListener property to capture these messages. The code example below demonstrates logging custom text and errors. :path=/static-assets/pdf/content-code-examples/how-to/javascript-to-pdf-message-listener.cs using IronPdf; using System; // Create a new instance of ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set up a listener for JavaScript console messages to help with debugging. // This logs messages from the PDF rendering process to the console. renderer.RenderingOptions.JavascriptMessageListener = message => Console.WriteLine($"JS: {message}"); // Log a message to the console from the JavaScript within the PDF rendering process. renderer.RenderingOptions.Javascript = "console.log('foo');"; // Render HTML to PDF and save the document within the PdfDocument object. // The HTML '<h1> Hello World </h1>' is converted to a PDF format. PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1> Hello World </h1>"); //--------------------------------------------------// // This block demonstrates handling an error scenario in JavaScript during the rendering process. // The attempt to style a non-existing element will yield a JavaScript error. // The error is logged thanks to the JavascriptMessageListener set previously. renderer.RenderingOptions.Javascript = "document.querySelector('non-existent').style.color='foo';"; // Render the same HTML to PDF to illustrate error logging. // Although the JavaScript will throw an error, the PDF rendering will still proceed. PdfDocument pdf2 = renderer.RenderHtmlAsPdf("<h1> Hello World </h1>"); Imports IronPdf Imports System ' Create a new instance of ChromePdfRenderer Private renderer As New ChromePdfRenderer() ' Set up a listener for JavaScript console messages to help with debugging. ' This logs messages from the PDF rendering process to the console. renderer.RenderingOptions.JavascriptMessageListener = Sub(message) Console.WriteLine($"JS: {message}") ' Log a message to the console from the JavaScript within the PDF rendering process. renderer.RenderingOptions.Javascript = "console.log('foo');" ' Render HTML to PDF and save the document within the PdfDocument object. ' The HTML '<h1> Hello World </h1>' is converted to a PDF format. Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1> Hello World </h1>") '--------------------------------------------------// ' This block demonstrates handling an error scenario in JavaScript during the rendering process. ' The attempt to style a non-existing element will yield a JavaScript error. ' The error is logged thanks to the JavascriptMessageListener set previously. renderer.RenderingOptions.Javascript = "document.querySelector('non-existent').style.color='foo';" ' Render the same HTML to PDF to illustrate error logging. ' Although the JavaScript will throw an error, the PDF rendering will still proceed. Dim pdf2 As PdfDocument = renderer.RenderHtmlAsPdf("<h1> Hello World </h1>") $vbLabelText $csharpLabel Charting with JavaScript & IronPDF D3.js is a perfect fit for IronPDF when you want to create charts or images. D3.js, known as Data-Driven Documents, is a JavaScript library for creating dynamic, interactive data visualizations in web browsers using Scalable Vector Graphics (SVG) in HTML5 and Cascading Style Sheets (CSS3). To create a chart using D3.js and then convert it into a PDF document using IronPDF, follow this process: Create a renderer object. Set JavaScript and CSS properties. Use the RenderHTMLFileAsPdf method to read an HTML file. Save it as a PDF. :path=/static-assets/pdf/content-code-examples/how-to/javascript-to-pdf-render-chart.cs // Import the IronPdf namespace to use the PDF generation functionality. using IronPdf; // Define an HTML string to be converted into a PDF. This contains a simple C3.js bar chart. string html = @" <!DOCTYPE html> <html> <head> <meta charset=""utf-8"" /> <title>C3 Bar Chart</title> </head> <body> <div id=""chart"" style=""width: 950px;""></div> <script src=""https://d3js.org/d3.v4.js""></script> <!-- Load c3.css --> <link href=""https://cdnjs.cloudflare.com/ajax/libs/c3/0.5.4/c3.css"" rel=""stylesheet""> <!-- Load d3.js and c3.js --> <script src=""https://cdnjs.cloudflare.com/ajax/libs/c3/0.5.4/c3.js""></script> <script> // Ensure backward compatibility for `bind` method in older browsers Function.prototype.bind = Function.prototype.bind function (thisp) { var fn = this; // Return a function that applies the original function (`fn`) using the provided context (`thisp`) return function () { return fn.apply(thisp, arguments); }; }; // Generate a simple chart using C3.js var chart = c3.generate({ bindto: '#chart', data: { columns: [ ['data1', 30, 200, 100, 400, 150, 250], ['data2', 50, 20, 10, 40, 15, 25] ] } }); </script> </body> </html> "; // Instantiate the ChromePdfRenderer which uses headless Chrome to render PDFs. var renderer = new ChromePdfRenderer(); // Enable JavaScript in the rendering process, important for rendering dynamic content. renderer.RenderingOptions.EnableJavaScript = true; // If the IronPdf library version requires it, set the JavaScript rendering option. renderer.RenderingOptions.JavaScript = IronPdf.RenderingOptionEnums.PdfRenderingOptionsJavascript.ManualRenderDelay; // Set the delay to 500 milliseconds, ensuring the chart is fully rendered before the PDF is generated. renderer.RenderingOptions.ManualJavaScriptDelay = 500; // Set the width of the output PDF to contain the full chart width. renderer.RenderingOptions.Width = 1000; // Render the HTML string (with embedded JavaScript) as a PDF document. var pdfJavaScript = renderer.RenderHtmlAsPdf(html); // Save the rendered PDF file to the specified path. pdfJavaScript.SaveAs("renderChart.pdf"); ' Import the IronPdf namespace to use the PDF generation functionality. Imports IronPdf ' Define an HTML string to be converted into a PDF. This contains a simple C3.js bar chart. Private html As String = " <!DOCTYPE html> <html> <head> <meta charset=""utf-8"" /> <title>C3 Bar Chart</title> </head> <body> <div id=""chart"" style=""width: 950px;""></div> <script src=""https://d3js.org/d3.v4.js""></script> <!-- Load c3.css --> <link href=""https://cdnjs.cloudflare.com/ajax/libs/c3/0.5.4/c3.css"" rel=""stylesheet""> <!-- Load d3.js and c3.js --> <script src=""https://cdnjs.cloudflare.com/ajax/libs/c3/0.5.4/c3.js""></script> <script> // Ensure backward compatibility for `bind` method in older browsers Function.prototype.bind = Function.prototype.bind function (thisp) { var fn = this; // Return a function that applies the original function (`fn`) using the provided context (`thisp`) return function () { return fn.apply(thisp, arguments); }; }; // Generate a simple chart using C3.js var chart = c3.generate({ bindto: '#chart', data: { columns: [ ['data1', 30, 200, 100, 400, 150, 250], ['data2', 50, 20, 10, 40, 15, 25] ] } }); </script> </body> </html> " ' Instantiate the ChromePdfRenderer which uses headless Chrome to render PDFs. Private renderer = New ChromePdfRenderer() ' Enable JavaScript in the rendering process, important for rendering dynamic content. renderer.RenderingOptions.EnableJavaScript = True ' If the IronPdf library version requires it, set the JavaScript rendering option. renderer.RenderingOptions.JavaScript = IronPdf.RenderingOptionEnums.PdfRenderingOptionsJavascript.ManualRenderDelay ' Set the delay to 500 milliseconds, ensuring the chart is fully rendered before the PDF is generated. renderer.RenderingOptions.ManualJavaScriptDelay = 500 ' Set the width of the output PDF to contain the full chart width. renderer.RenderingOptions.Width = 1000 ' Render the HTML string (with embedded JavaScript) as a PDF document. Dim pdfJavaScript = renderer.RenderHtmlAsPdf(html) ' Save the rendered PDF file to the specified path. pdfJavaScript.SaveAs("renderChart.pdf") $vbLabelText $csharpLabel Output PDF Explore more WaitFor options, such as those for fonts, JavaScript, HTML elements, and network idle at 'How to Use the WaitFor Class to Delay C# PDF Rendering.' AngularJS HTML to PDF AngularJS is a JavaScript-based open-source front-end web framework. It simplifies the development and testing of single-page applications using client-side model–view–controller (MVC) and model–view–view model (MVVM) architectures. For full compatibility, consider using server-side rendering (SSR) together with Angular Universal. Server-side rendering (SSR) with Angular Universal Angular vs. Angular Universal A typical Angular application runs in the browser (client-side), rendering pages in the DOM (Document Object Model) based on user actions. Angular Universal runs on the server (server-side) and generates static pages that are bootstrapped on the client. This improves loading time and allows users to view the layout before it becomes fully interactive. Frequently Asked Questions What is this .NET library used for converting HTML to PDF, including executing JavaScript? IronPDF is a .NET library that supports converting HTML to PDF, including the execution of JavaScript and jQuery, using the Chromium rendering engine. How can I enable JavaScript execution in this .NET PDF conversion library? JavaScript execution can be enabled in IronPDF by setting the `EnableJavaScript` property in the rendering options. What is the purpose of configuring rendering options for JavaScript execution in a .NET library? The WaitFor class in IronPDF is used to configure rendering options to allow JavaScript time to execute properly, ensuring that all scripts are run before the PDF is generated. Can this .NET library capture JavaScript console messages during PDF conversion? Yes, IronPDF can capture JavaScript console messages using the `JavascriptMessageListener` property. How does this .NET library interact with D3.js for rendering charts? IronPDF can render charts created with D3.js by enabling JavaScript in the rendering options and using the `RenderHtmlFileAsPdf` method. What are some limitations of using JavaScript with this .NET PDF conversion tool? Some complex JavaScript frameworks might not work perfectly with IronPDF due to limited memory allocation for JavaScript execution. What is the benefit of using Angular Universal with this .NET PDF conversion library? Angular Universal improves loading times and allows users to view layouts before they become fully interactive, ensuring better performance and compatibility when rendering with IronPDF. How can I execute custom JavaScript when using a .NET library for converting HTML to PDF? Custom JavaScript can be executed by assigning the JavaScript code to the `JavaScript` property in the rendering options before rendering HTML to PDF. What is the recommended wait time for JavaScript execution in this .NET PDF library? While configuring the WaitFor class, a typical maximum wait time for JavaScript execution is 500 ms to ensure proper execution before rendering. How can server-side rendering benefit an AngularJS application using this .NET PDF conversion library? Server-side rendering with Angular Universal can enhance an AngularJS application's performance by pre-rendering pages on the server, thus improving load times and enabling IronPDF to effectively convert these pages to PDF. Chaknith Bin Chat with engineering team now Software Engineer Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience. Ready to Get Started? Free NuGet Download Total downloads: 14,143,061 View Licenses