WebSave as PDF Documentation

Overview

WebSave as PDF is a JavaScript integration for adding a PDF download button to a website. When a visitor clicks the button, PDFCrowd creates a PDF from the webpage, using either its URL or the current page HTML.

Quick Start

Use this starter code to add the WebSave button.

The starter code includes PDFCrowd's demo key for testing. If you are signed in and have a WebSave key, the example uses your selected key instead. See key setup.

Starter Code

Paste this block where the Save as PDF button should appear.

<!-- Button shown on your page -->
<button class="pdfcrowd-websave pdfcrowd-websave-style"
        data-key="demo"
        data-config="webSaveConfig">
  Save as PDF
</button>

<!-- Recommended PDF settings -->
<script>
window.webSaveConfig = {
  apiSettings: {
    content_viewport_width: 'balanced'
  }
};
</script>

<!-- Load WebSave once, after the button and configuration -->
<script src="https://edge.pdfcrowd.com/websave/1.4.2/websave.min.js" async></script>
  • pdfcrowd-websave enables WebSave on the button.
  • pdfcrowd-websave-style adds default styling, icon, and spinner.
  • data-config points to window.webSaveConfig.

Choose URL or Content Mode

The starter code uses URL mode. Both modes work for public pages; use content mode when you need the visitor's current page content.

Scenario URL Mode (default) Content Mode
Best for Public pages Forms, charts, and application results already loaded in the browser
How it works PDFCrowd fetches the page URL Browser sends current page HTML
Behind login Does not share the visitor's login session Sends the HTML already loaded in the browser; protected assets may need extra handling
Form values, user input Visitor's current input is not sent Includes current form values and selections

See Data security for how PDFCrowd handles page content in both modes.

Use Content Mode

Set conversionMode: "content" in your JavaScript configuration:

<!-- Button with content mode configuration -->
<button class="pdfcrowd-websave pdfcrowd-websave-style"
        data-key="demo"
        data-config="webSaveConfig">
  Save as PDF
</button>

<script>
window.webSaveConfig = {
  // Enable content mode to capture current DOM state
  conversionMode: 'content',
  apiSettings: {
    content_viewport_width: 'balanced'
  }
};
</script>

<script src="https://edge.pdfcrowd.com/websave/1.4.2/websave.min.js" async></script>

Content mode captures the current page, including form values and visible canvas graphics. Load the content you want to include before conversion. PDFCrowd renders this captured content to create the output.

Initialize Buttons Rendered After Page Load

If the DOM is ready when the WebSave script runs, it initializes existing buttons immediately. Otherwise, it waits for DOMContentLoaded.

For buttons added after this automatic initialization, call WebSave.initButton() once the button is in the DOM:

window.WebSave.initButton(document.querySelector('#save-as-pdf'));

This applies to React, Vue, Angular, Svelte, and other apps that create DOM elements after the initial page load.

See PDF export in reactive apps for framework-specific components and setup.

Recipes

Use these optional recipes when you need customization or advanced behavior.

Customize the PDF Output

Set the filename and page layout in your button's configuration.

WebSave options such as fileName and fileAction go at the top level of the configuration object. Put conversion options inside apiSettings.

<button class="pdfcrowd-websave pdfcrowd-websave-style"
        data-key="demo"
        data-config="webSaveConfig">
  Save as PDF
</button>

<script>
window.webSaveConfig = {
  fileName: 'report.pdf',
  fileAction: 'download',
  apiSettings: {
    content_viewport_width: 'balanced',
    page_size: 'Letter',
    margin_top: '0.75in',
    margin_bottom: '0.75in'
  }
};
</script>

<script src="https://edge.pdfcrowd.com/websave/1.4.2/websave.min.js" async></script>

Common settings:

Purpose Settings
Output filename fileName
Download or open the file fileAction
Page size and orientation page_size, orientation
Page margins margin_top, margin_right, margin_bottom, margin_left
Headers and footers header_html, footer_html
Layout and scaling content_viewport_width, scale_factor

For all available options, see the WebSave reference and the PDF conversion reference.

To let visitors choose PDF options before downloading, see PDF settings dialog.

Use the Playground configuration tool to adjust margins, headers, page layout, and other settings, then generate ready-to-use WebSave code. Best used on desktop.

Customize Button Appearance

Change the text inside the <button> element to set its label. Use icon, iconPosition, and spinner at the top level of the configuration object to adjust the built-in appearance.

To use your site's button styling, remove pdfcrowd-websave-style and add your own CSS class. Keep pdfcrowd-websave so WebSave recognizes the button.

Create a Print-Friendly PDF

Use custom_css to hide navigation, sidebars, or other elements in the generated PDF. These styles apply only during conversion.

Replace .site-nav and .sidebar in this example with CSS selectors for the elements on your page.

<button class="pdfcrowd-websave pdfcrowd-websave-style"
        data-key="demo"
        data-config="webSaveConfig">
  Save as PDF
</button>

<script>
window.webSaveConfig = {
  apiSettings: {
    content_viewport_width: 'balanced',
    custom_css: `
      .site-nav, .sidebar {
        display: none !important;
      }
    `
  }
};
</script>

<script src="https://edge.pdfcrowd.com/websave/1.4.2/websave.min.js" async></script>

Other ways to choose what appears:

Purpose Approach
Use your print stylesheet Set use_print_media to true to apply your site's @media print rules.
Exclude selected elements Add the pdfcrowd-remove class to those elements in your HTML.
Save only part of a page Set element_to_convert to a selector such as #result. Use element_to_convert_mode with 'remove-siblings' to keep the element's ancestors for CSS matching.

For content updated in the browser, use Content Mode. Element selection changes the output; Content Mode still sends the page HTML for conversion.

For advanced changes, custom_javascript can modify content before rendering. In URL Mode, onBeforeConversion can select a print URL provided by your site.

Respond to Conversion Events

Add callbacks to your configuration to update your page, record analytics, or handle the generated file yourself.

CallbackPurpose
onBeforeConversion Adjust settings before conversion, or return true to cancel it.
onFileCreated Use the generated file or record a successful conversion. Return true to skip the default download or open action.
onError Log an error or show your own message. Return true to hide the default error dialog.

Return false or omit the return value to keep WebSave's normal behavior. This example displays a message when the PDF is ready and lets WebSave download it.

<button class="pdfcrowd-websave pdfcrowd-websave-style"
        data-key="demo"
        data-config="webSaveConfig">
  Save as PDF
</button>
<p id="save-status" role="status"></p>

<script>
window.webSaveConfig = {
  onFileCreated: function() {
    document.getElementById('save-status').textContent = 'Your PDF is ready.';
    return false; // Continue with the download.
  },
  apiSettings: {
    content_viewport_width: 'balanced'
  }
};
</script>

<script src="https://edge.pdfcrowd.com/websave/1.4.2/websave.min.js" async></script>

For a custom error message, see Custom Error Handling.

Authentication

With an active trial or paid license, you can create a WebSave key for your website. Each key is tied to the domain or subdomain you specify.

  1. Create a key on your WebSave Keys page, specifying your website's domain.
  2. Set the button's data-key attribute to that key.

With Include subdomains enabled, the same key also works on subdomains of the site you enter.

See Authentication and API Keys for account setup and domain scope details.

Error Handling

WebSave shows an error dialog if a conversion fails after any automatic retries. Custom error handling is optional.

Built-in Behavior

  • Retries conversion requests that fail with HTTP 502/503 up to twice (three attempts total).
  • Disables buttons with the pdfcrowd-websave-style class during conversion and re-enables them afterwards.
  • Ignores additional conversion requests while a conversion is running.

Custom Error Handling

Use the onError callback to display your own message. This example shows an inline message, logs diagnostic details to the browser console, and clears the message when a new conversion starts. Return true to suppress WebSave's default error dialog.

<button class="pdfcrowd-websave pdfcrowd-websave-style"
        data-key="demo"
        data-config="webSaveConfig">
  Save as PDF
</button>
<p id="save-error" role="alert"></p>

<script>
const errorElement = document.getElementById('save-error');
window.webSaveConfig = {
  onBeforeConversion: function() {
    errorElement.textContent = '';
  },
  onError: function(message, status) {
    console.error('WebSave error:', message, status);
    errorElement.textContent = 'Unable to create the PDF. Please try again.';
    return true; // Suppress the default error dialog.
  },
  apiSettings: {
    content_viewport_width: 'balanced'
  }
};
</script>

<script src="https://edge.pdfcrowd.com/websave/1.4.2/websave.min.js" async></script>

For detailed error code information, see the Status Codes Reference.

Troubleshooting

This guide helps you diagnose and resolve common WebSave integration issues. For automatic error recovery and customization options, see the Error Handling section above.

Button Not Working

Button doesn't respond to clicks

  • Verify the button has the correct class: pdfcrowd-websave
  • Check that data-key attribute contains your WebSave key
  • Ensure the WebSave script loads
  • Confirm your page is served over HTTPS - WebSave requires secure connections
  • Open browser console and check for JavaScript errors or security warnings

Invalid WebSave key error

  • Verify the key matches the domain or subdomain where your page is hosted
  • Check the key scope in your WebSave dashboard: "Exact only" keys do not work on subdomains
  • For example, a key for example.com works on www.example.com only when "Include subdomains" is enabled
  • A key for www.example.com with "Include subdomains" covers deeper subdomains such as app.www.example.com, not sibling sites such as dev.example.com
  • Check that you're using a WebSave key (not a regular API key)
  • Ensure the key is active

Works on one subdomain but not another

WebSave key scope follows the exact site entered when the key is created. A key for example.com with "Include subdomains" works on www.example.com and dev.example.com. A key for www.example.com works on www.example.com and, with "Include subdomains", on deeper subdomains such as app.www.example.com. It does not work on sibling sites such as dev.example.com.

Custom URL mode target rejected

In URL mode, WebSave normally converts the current page URL. If your JavaScript changes the URL to be converted, the browser same-origin check still uses exact host matching. example.com and www.example.com are different origins, even if the WebSave key includes subdomains. Use content mode when you need to convert the current browser page state.

Not working on localhost

WebSave cannot access localhost URLs as conversions happen on PDFCrowd servers. Use ngrok or localtunnel to create a temporary public HTTPS URL for your local development server.

Button not working in reactive frameworks

Content Not Captured Correctly

Pages behind login or with user input

Use content mode to include content from the visitor's logged-in session or current form values.

JavaScript-generated or AJAX content missing

For content already loaded in the browser, use content mode. In URL mode, add javascript_delay to allow time for page scripts to finish:

window.webSaveConfig = {
  apiSettings: {
    javascript_delay: 2000,  // Wait 2 seconds for JS/AJAX
  }
}

Wrong responsive layout or content cut off

PDFs may show mobile layout instead of desktop (or vice versa) based on viewport width. Set content_viewport_width to control which responsive breakpoint is used:

window.webSaveConfig = {
  apiSettings: {
    content_viewport_width: "1280px",  // Force desktop layout
  }
}

Common widths: Mobile (375-414px), Tablet (768-834px), Desktop (1280-1920px).

Debugging Tools and Techniques

WebSave provides two debugging options to help diagnose different types of issues:

  • verbosity logs WebSave events (button clicks, API calls, key validation) to your browser console - useful for integration and configuration issues.
  • debug_log captures what happens on PDFCrowd servers (resource loading, timeouts) - helpful for content and layout problems. Find these logs in your conversion log.
window.webSaveConfig = {
  verbosity: 1,  // Client-side: logs events to browser console
  apiSettings: {
    debug_log: true,  // Server-side: saves details to conversion log
  }
};

Getting Help

For more guidance, see the API FAQ and WebSave Reference.

We’re happy to help you add the button, choose the right settings and fine-tune the output for your site. When contacting us, include:

  • Your page URL or a test page
  • Your WebSave configuration code
  • A brief description of the result you want

For conversion errors, include the error message and any available debug logs.

Get help with your setup