skip to Main Content

I have a webpage that is fully built out and displays exactly as needed. I received a requirement from my boss to now take that same page and turn it into a PDF. I have a rather old installation of DomPDF and upgrading is not an option due to PHP being 5.6.40 and newer versions of DomPDF require PHP 7.1 or greater. Additionally, I have an equally old version of fPdf on the host. Also, all the docs for DomPDF seem to elude to me needing to have all my HTML in a variable to be used by DomPDF. Again, not an option as the page is already fully built out and very jQuery intensive.

So, what options do I have for converting an existing HTML page to a PDF?

I desperately need a solution to this. Thanks everyone!

2

Answers


  1. Chosen as BEST ANSWER

    I was able to get it to work using the following strategy;

    $f = file_get_contents("URL to page I wish to convert");
    $dompdf->loadHtml($f);
    

    This worked great all except for the rather painful shortcomings that dompdf has with pulling in styling properly.


  2. How about using jsPDF extension, it runs on jQuery too and converts HTML to PDF without any server side effort. I am not much into PHP and never used DomPDF as well. Also we can have different CSS styles to the page elements for printing to PDF to make sure all contents fit the view.

    var doc = new jsPDF(); 
    var specialElementHandlers = { 
        '#editor': function (element, renderer) { 
            return true; 
        } 
    };
    $('#submit').click(function () { 
        doc.fromHTML($('#content').html(), 15, 15, { 
            'width': 190, 
                'elementHandlers': specialElementHandlers 
        }); 
        doc.save('sample-page.pdf'); 
    });
    

    Check the working Codepen example
    (https://codepen.io/connectpritam/pen/wvKqwLG)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search