Search code examples
javascriptphplaravelhighchartsdompdf

JavaScript in laravel-dompdf


I am using laravel-dompdf to generate a PDF with a summary of results, consisting of tables and a few images, nothing to spectacular. I would like to print highcharts in the generated PDF as well. So first, I want to try a simple javascript code to see if it was working... but it isn't. I have enabled the "DOMPDF_ENABLE_JAVASCRIPT" => true, but so far no luck yet. The simple code which I want to print:

<div>foo</div>
<span id="insertHere"></span>
<div>bar</div>

<script>
    var el = document.getElementById('insertHere');
    el.innerHTML = '<div>Print this after the script tag</div>';
</script>

This only prints the foo and bar.. Could someone please help me out?


Solution

  • Dompdf (or more specifically Back-end that it uses for PDF rendering) doesn't really render javascript. What it does is - it parse everything inside <script type="text/javascript"></script> tag and adds it in the special section of the pdf file.

    I'm not sure if this javascript is what you need, since you can work with the dom tree outside of the <script> tag. What you can do is write simple scenarios, for example like this one:

    <div>foo</div>
        <span id="insertHere"></span>
    <div>bar</div>
    
    <script type="text/javascript">
        app.alert({cMsg:"Message", cTitle: "Title"});
    </script>
    

    Try to open generated file and you will see alert.

    Here more complete guide on what you can do inside javascript section: Acrobat JavaScript Scripting Guide