Convert HTML to PDF using Javascript

Feel free to use this web service to create PDF's from just Javascript and HTML. You can use the platform for either commercial or personal use.

Note that using javascript means your API key will be visible and if you are worried about another HTML to PDF Rocket customer grabbing it and coding their own service, you should use a server side approach.

Our API will reading your webpage including any linked CSS, fonts etc (or it can accept HTML directly) and dynamically generate a PDF or an image. While it can handle complex documents and layouts including images and executing javascript, obviously try to keep your page as simpler as possible.

    • Example #1 - Open the PDF in a new tab

    • 
      /**
      * html: HTML string to convert to PDF
      * savePdf: Callback for saving PDF
      * Opens the PDF in a new tab, and returns it as a data URI
      */
      function pdfRocket(html, savePdf) {
             var self = this;
            
             self.save = savePdf;
             self.req = new XMLHttpRequest();
       
             var url = "http://api.html2pdfrocket.com/pdf";
             var apiKey = "ABCD-1234";
      
             // Additional parameters can be added here
             var data = "apikey=" + apiKey + "&value=" + encodeURIComponent(html);
       
             self.req.onload = function(event) {
                    self.reader = new FileReader();
                   
                    self.reader.addEventListener("loadend", function() {
      
                           // Open in new tab
                           window.open(self.reader.result, "_blank");
                          
                           // return data URI
                           return self.reader.result;
                    });
                   
                    self.reader.readAsDataURL(self.req.response);
             };
       
             self.req.open("POST", url, true);
             self.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
             self.req.responseType = "blob";
       
             self.req.send(data);
      }
       
      
       
      
    • Example #2 - How to get the binary PDF data

    •                                          
      /**
      * html: HTML string to convert to PDF
      * savePdf: Callback for saving PDF
      * Returns the binary PDF data
      */
       
      function pdfRocket(html, savePdf) {
             var self = this;
            
             self.save = savePdf;
             self.req = new XMLHttpRequest();
       
             var url = "http://api.html2pdfrocket.com/pdf";
             var apiKey = "ABCD-1234";
             // Additional parameters can be added here
             var data = "apikey=" + apiKey + "&value=" + encodeURIComponent(html);
       
             self.req.onload = function(event) {
                    self.save(self.req.response);
             };
       
             self.req.open("POST", url, true);
             self.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
             self.req.responseType = "blob";
       
             self.req.send(data);
      }
      

Need another Javascript example? Would you mind dropping us a quick one line note to say which example you would have liked and we'll put the most commonly used ones here.

Get Started

Don't waste your day reinventing the wheel, be converting in 3 minutes from now

Conversion Tip


The number #1 reason a conversion fails to render how you want is because it contains an references to external links or images that doesn't exist. If there is a conversion error, please check the page using your webbrowsers 'developer' network tab to ensure there are no 404 errors.