JavaScript Message Listener

With IronPDF's ChromePdfRenderOptions.JavascriptMessageListener property, you can add a method callback to be invoked whenever a browser console message becomes available. This can be particularly useful for debugging or handling specific client-side events. Learn more about this feature by visiting the IronPDF Documentation on Javascript Console Messages.

// Import IronPdf library
using IronPdf;

public class PdfGenerator
{
    public static void Main()
    {
        // Initialize ChromePdfRenderOptions
        ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();

        // Define a callback method to handle JavaScript console messages
        renderOptions.JavascriptMessageListener = OnJavascriptMessage;

        // Create a new HtmlToPdf instance using the customized render options
        HtmlToPdf htmlToPdf = new HtmlToPdf(renderOptions);

        // Define the HTML content you want to convert to PDF
        string htmlContent = "<html><body><script>console.log('PDF generation started.');</script><h1>Hello World</h1></body></html>";

        // Convert HTML content to a PDF document
        PdfDocument pdf = htmlToPdf.RenderHtmlAsPdf(htmlContent);

        // Save the generated PDF to a file
        pdf.SaveAs("output.pdf");
    }

    // Callback method to process JavaScript console messages
    private static void OnJavascriptMessage(string message)
    {
        // Output message to the system console
        Console.WriteLine("JavaScript message: " + message);
    }
}
// Import IronPdf library
using IronPdf;

public class PdfGenerator
{
    public static void Main()
    {
        // Initialize ChromePdfRenderOptions
        ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();

        // Define a callback method to handle JavaScript console messages
        renderOptions.JavascriptMessageListener = OnJavascriptMessage;

        // Create a new HtmlToPdf instance using the customized render options
        HtmlToPdf htmlToPdf = new HtmlToPdf(renderOptions);

        // Define the HTML content you want to convert to PDF
        string htmlContent = "<html><body><script>console.log('PDF generation started.');</script><h1>Hello World</h1></body></html>";

        // Convert HTML content to a PDF document
        PdfDocument pdf = htmlToPdf.RenderHtmlAsPdf(htmlContent);

        // Save the generated PDF to a file
        pdf.SaveAs("output.pdf");
    }

    // Callback method to process JavaScript console messages
    private static void OnJavascriptMessage(string message)
    {
        // Output message to the system console
        Console.WriteLine("JavaScript message: " + message);
    }
}
$vbLabelText   $csharpLabel

Explanation of the Code

  • Importing IronPdf: The code begins by importing the necessary IronPdf library which allows for PDF generation.
  • PdfGenerator Class: This is the main class that contains the PDF generation code.
  • ChromePdfRenderOptions: A ChromePdfRenderOptions object is initialized to configure how PDFs are rendered. This includes setting the JavascriptMessageListener.
  • JavascriptMessageListener Callback: The JavascriptMessageListener property is set to a custom callback method, OnJavascriptMessage. This will be triggered whenever a JavaScript console message is executed within the HTML during PDF creation.
  • HtmlToPdf Instance: The HtmlToPdf object is created using the configured render options, allowing conversion from HTML content to a PDF.
  • HTML Content: The HTML content contains a JavaScript console log that serves as a demonstration for triggering the message listener.
  • PdfDocument: The HtmlToPdf.RenderHtmlAsPdf method is used to create a PDF document from the specified HTML content.
  • Saving the PDF: The generated PDF is saved as "output.pdf" using the SaveAs method.
  • OnJavascriptMessage Method: This is the callback method implemented to handle JavaScript messages. It outputs the incoming messages to the console, aiding in debugging and analysis.

This code example demonstrates how to integrate JavaScript console message handling within the PDF generation process using IronPDF in C#. This feature can be beneficial for troubleshooting or logging purposes as you render dynamic HTML content.