HTML-to-PDF with jQuery


Code Examples

jQuery & JavaScript

The DocRaptor JavaScript library makes it easy to create PDFs with JavaScript. The library does not require jQuery, but you can use jQuery to define your document content. When a PDF is requested, the library constructs a hidden form and submits it to the DocRaptor API. Until all modern browsers support the download link attribute, using this hidden form is the best way to generate a file download directly from JavaScript.

Warning: This code exposes your API key in your website source code. This code should not be used in a publicly-accessible location, instead try using a server-side agent such as PHP or Ruby.

Example Code

    <script src=""></script>
      var downloadPDF = function() {
        DocRaptor.createAndDownloadDoc("YOUR_API_KEY_HERE", {
          test: true, // test documents are free, but watermarked
          type: "pdf",
          document_content: document.querySelector('html').innerHTML, // use this page's HTML
          // document_content: "<h1>Hello world!</h1>",               // or supply HTML directly
          // document_url: "",            // or use a URL
          // javascript: true,                                        // enable JavaScript processing
          // prince_options: {
          //   media: "screen",                                       // use screen styles instead of print styles
          // }
      @media print {
        #pdf-button {
          display: none;
    <input id="pdf-button" type="button" value="Download PDF" onclick="downloadPDF()" />

We've got a DocRaptor jQuery plugin that you should check out. Examples and usage details can be found on that page.
Note: if you don't want to expose your API key in your client-side JavaScript, check out Referrer-based Document Generation. Also, DocRaptor fully supports the CORS specification, allowing cross-site HTTP requests.

With DocRaptor, it's fast and painless to convert HTML, CSS, and JavaScript into PDF and XLS documents with jQuery. Below are working jQuery examples for creating documents. Our API reference lists all the generation options and our style and formatting guide will help make it look perfect.