Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
JavaScript

How to Render a Webpage to PDF with PhantomJS—and When You Need a Download Instead

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To render a webpage as a PDF with PhantomJS, load it with page.open(), set page.paperSize if you need a specific page layout, and call page.render('output.pdf') after the load succeeds. That creates a PDF from the page PhantomJS rendered. It does not download an existing PDF response unchanged: if a URL already serves the PDF file you need, retrieve it with an HTTP client instead.

PhantomJS is a legacy choice: its project says development is suspended, and its GitHub repository has been archived read-only since May 30, 2023. The examples below are for maintaining an existing workflow, not a claim that PhantomJS is a maintained browser for new projects.

Render a webpage to PDF with PhantomJS

The basic sequence is: create a webpage object, optionally configure its paper size, open the target URL, check the callback status, and render to a filename ending in .pdf. PhantomJS’s page.render() selects the output format from the filename extension and supports PDF.

This minimal illustration follows the documented API order; it is not presented as a tested program. Replace the example URL and output path as needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Epson EcoTank ET-2800 Wireless Color All-in-One Supertank Printer - Black
  • INNOVATIVE CARTRIDGE-FREE PRINTING — No more dealing with lots of tiny ink cartridges; With this wireless document and photo printer each ink bottle set is equivalent to about 90 individual cartridges²
  • LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; When you choose this combination printer, scanner and copier you can print up to 4,500 pages black/7,500 color³
  • COLOR PRINTING — Up to 2 years of ink in the box4 (and with every replacement ink set) for fewer out-of-ink frustrations
  • ZERO CARTRIDGE WASTE — By using an Epson EcoTank printer you can help reduce the amount of cartridge waste ending up in landfills
  • HOME PRINTER DESIGNED FOR RELIABILITY — The Epson EcoTank ET-2800 All-in-One Supertank Color Printer creates vivid, detailed prints and documents thanks to Micro Piezo Heat-Free Technology; Fire off 10 ISO pages per minute1 to easily finish large jobs
var page = require('webpage').create();

page.paperSize = {
  format: 'A4',
  orientation: 'portrait',
  margin: '1cm'
};

page.open('https://example.com', function (status) {
  if (status !== 'success') {
    console.log('Unable to load page');
    phantom.exit(1);
    return;
  }

  page.render('output.pdf');
  phantom.exit();
});

In a PhantomJS script, require('webpage').create() creates the page object. The page.open() callback receives a status; only proceed to render when it is success. A failed load should not be treated as a successful PDF capture: log or report the failure and exit with a nonzero status, as above.

Wait for the content you actually need

A successful page.open() callback tells you the page load succeeded; it does not promise that every site has finished fetching and drawing its client-side data. If the page fills in content asynchronously, rendering immediately may capture an incomplete state. Add a site-specific readiness check—such as waiting for an element that appears only when the needed data is ready—before calling page.render(). The exact condition depends on the site and is not a universal PhantomJS setting.

Choose the PDF page size and layout

Set page.paperSize before rendering when the PDF needs defined dimensions, orientation, or margins. If you leave it unset, the webpage determines the size. PhantomJS documents named formats, explicit dimensions, margins, orientation, and repeating headers and footers.

Setting Documented behavior
format Named formats include A3, A4, A5, Legal, Letter, and Tabloid.
width and height Explicit dimensions may use mm, cm, in, or px. A number without a unit is interpreted as pixels.
orientation portrait or landscape; portrait is the default.
margin Margins default to zero. You can use one value for all sides or an object with top, left, bottom, and right values.
Headers and footers The API supports repeating headers and footers with a height and callback-generated contents.

For example, replace the paperSize object in the first script with explicit dimensions when a named format is not appropriate:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
page.paperSize = {
  width: '210mm',
  height: '297mm',
  margin: {
    top: '12mm',
    right: '10mm',
    bottom: '12mm',
    left: '10mm'
  }
};

Use either a named format or dimensions that match your output needs, then inspect the resulting PDF for clipping, scaling, and page breaks. A page size setting cannot guarantee that every site’s layout will fit cleanly.

Rank #2
Sale
Epson EcoTank Photo ET-8550 Wireless Wide-Format All-in-One Tank Printer
  • CARTRIDGE-FREE PRINTING — Print lab-quality photos, graphics and creative projects; Get vibrant colors and sharp text with Epson's high-accuracy printhead and Claria ET Premium 6-color inks
  • INK BOTTLES — Save on photos1 and creative projects with affordable in-house printing; All-in-one printer allows you to print 4" x 6" photos for about 4 cents each vs. 40 cents with traditional ink cartridges1
  • LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; Printer, scanner and copier lets you print up to 6,200 color pages³
  • PRINT FOR LONGER — Up to 2 years of ink in the box² (and with every replacement ink set) for fewer out-of-ink frustrations with this wireless printer
  • ZERO CARTRIDGE WASTE — Epson EcoTank printer helps reduce the amount of cartridge waste ending up in landfills; Cartridge-free printer uses high-yield ink bottles; Each replacement ink bottle set is equivalent to about 100 individual ink cartridges⁴

Do not use render quality to tune PDF text

The optional quality argument to page.render() concerns JPEG and PNG output. For a PDF, configure the paper size and margins through page.paperSize; changing image quality is not a way to improve PDF text or vector output.

Check the page background

The official PhantomJS FAQ warns that a page without a defined background may render transparently. If your PDF looks transparent or its background is missing, inspect the target page’s CSS and confirm the rendered result. The FAQ’s workaround is to set the page body background color before rendering, for example:

page.evaluate(function () {
  document.body.style.backgroundColor = '#ffffff';
});
page.render('output.pdf');

Render HTML already in memory

If your script already has the HTML string, use page.setContent(html, baseUrl) instead of navigating to a page with page.open(). This loads the supplied content and sets the current URL without making an HTTP request. Then set the PDF options and render as usual.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
var page = require('webpage').create();
var html = '<!doctype html><html><body><h1>Report</h1><p>Generated locally.</p></body></html>';

page.paperSize = {
  format: 'A4',
  orientation: 'portrait',
  margin: '1cm'
};
page.setContent(html, 'https://example.com/');
page.render('report.pdf');
phantom.exit();

Choose a meaningful baseUrl if the HTML refers to relative stylesheets, images, or other resources. It can help those paths resolve as intended, but resource loading still depends on the content and environment; check the PDF rather than assuming every reference loaded.

Render a page or download an existing PDF?

These are different jobs. Use page.open(url) followed by page.render('output.pdf') when the input is a webpage and the desired output is a PDF rendering of that page. Use a normal HTTP client or file-download mechanism when the URL already returns the PDF file you want to save. The documented page.render() method writes a rendered page to a filename; it is not documented as a way to preserve and save an existing PDF response as-is.

Rank #3
HP Smart Tank 5000 Wireless All-in-One Ink Tank Printer, Scanner, Copier with 2 Years of Ink Included, Best-for-Home, Cartridge-Free, Refillable and AI-Enabled. (5D1B6A)
  • SET IT UP ONCE AND PRINT WITH CONFIDENCE. No complicated maintenance. Just easy, reliable printing you can count on.
  • INK FOR YEARS. NOT MONTHS. Up to 2 years of ink included. Get thousands of pages of cartridge-free printing. More pages, less hassle
  • KEEPS PRINTING WELL AFTER COMPETITORS HAVE QUIT. No complex maintenance. Sharper text, richer colors.[2] Only with HP Smart Tank
  • PREMIUM SUPPORT - Strong technical expertise to solve issues faster
  • THE LAST PRINTER YOU'LL EVER NEED. Enjoy years of refillable, cartridge-free printing.

For an existing PDF URL, handle the HTTP response as a file and validate the request separately. In particular, check whether the server redirects, requires authentication or special headers, and returns a successful response containing the expected file. A browser rendering call and an HTTP file download have different response-handling requirements.

When to keep PhantomJS—and its limits

The PhantomJS project README states, “Important: PhantomJS development is suspended until further notice”. GitHub marks the project repository archived and read-only as of May 30, 2023. That status matters if you depend on the browser engine continuing to accommodate changing websites: modern pages can rely on markup, CSS, scripts, and resources that a legacy engine may not handle as expected.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • For an existing, stable workflow: test representative target pages and verify the produced PDFs, including asynchronous content and backgrounds.
  • For a new or reliability-sensitive workflow: evaluate a maintained browser automation option rather than assuming PhantomJS will keep up with the pages you need to capture.
  • For repeatable output: define the paper size and margins, establish a readiness condition for dynamic pages, and check the actual file after rendering.

The official PhantomJS examples page lists rasterize.js under “Rendering/rasterization” as an end-to-end example that can rasterize a page to an image or PDF. Use it as a reference for the overall workflow, and consult the API documentation for the precise behavior of individual options.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting PhantomJS PDF output

The script exits without creating a PDF

Check the page.open() callback status first. The example deliberately renders only on success; on fail, it reports the load problem and exits with status 1. Verify the URL and any access requirements, then make sure the script’s output path is writable.

The PDF is blank or missing page content

Do not assume a successful load means the page’s asynchronous content is ready. Wait for the page-specific element or state that indicates the required content has appeared, then render. If the page is supplied HTML, check that its linked resources resolve from the chosen base URL.

Rank #4
NDYIN Portable Printers Wireless for Travel, N80 Bluetooth Thermal Printer
  • Wireless Bluetooth Printer: Portable thermal printer compatible with iPhone, Android phones, iPad and tablet computers via Bluetooth. For smartphones, please download the "Nada Print" App. You can also connect to laptops and computers for printing using a USB-C cable. (Note: Laptops and computers can only be connected via USB and require the installation of a driver first. Bluetooth connection is not supported.)
  • No-ink printing: Only supports US Letter and A4 size thermal paper.(Doesn't support regular paper) The no-ink portable thermal printer uses direct thermal technology, requiring no ink, toner or ribbons, making it environmentally friendly, cost-effective and time-saving. The thermal printer package comes with a roll of US Letter thermal printing paper. Note: When installing the paper, remember to switch the paper size switch on APP
  • Clear Print: NDYIN N80 portable thermal printer adopts high-definition printing technology, with a 203DPI resolution to provide you with clear printing results. This mobile printer is compatible with roll paper, folded paper and tattoo transfer paper, supporting printing from your mobile phone PDF, Word, pictures and web pages anytime and anywhere. It is recommended to use our NDYIN thermal paper to achieve good printing quality
  • Portable wireless printer for travel: The thermal printer is equipped with a built-in 1500mAh rechargeable battery, which can print 160 sheets of 8.5" x 11" thermal paper after being fully charged. It weighs only 1.5 pounds and is compact in size. This ink-free portable printer can be easily carried in a backpack or briefcase! It is perfect for business travel, cars, small offices, construction sites, schools and homes. You can print documents, contracts, invoices and boarding passes anytime and anywhere
  • The N80 thermal printer has a wide range of uses. The package includes the N80 printer, a roll of US Letter paper(7m/roll), a user manual, a guide card, a type-C soft cable and a type C adapter. Note: The charging adapter is not included. Special thermal paper is required for use; ordinary paper cannot be used. This ink-free portable thermal printer is suitable for various scenarios such as home, school, travel, office, and outdoor, meeting the printing needs of different groups of people. This tattoo template printer is also compatible with tattoo transfer paper, making it an ideal choice for tattoo art

The dimensions or margins are wrong

Set page.paperSize before rendering. Confirm whether you chose a named paper format or explicit dimensions, that each dimension has the intended unit, and that the margins are not being left at their zero default. Inspect page breaks and clipping in the resulting file.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The background appears transparent

Inspect the page’s background CSS and the resulting PDF. The PhantomJS FAQ notes that a page without a defined background may render transparently; setting the body background before rendering is its documented workaround.

Changing quality does not improve the PDF

The render quality argument applies to JPEG and PNG output. Adjust PDF page dimensions and margins with paperSize, and check the source page’s styles and layout rather than treating quality as a PDF-text setting.

Or skip the browser setup

If your goal is to capture a webpage rather than maintain PhantomJS, ScreenshotNeo offers a screenshot API and MCP server. It supports PDF output, and its cleanup options can accept cookie/consent banners and remove known consent platforms, newsletter popups, and chat widgets before capture. The example below is the supplied one-call screenshot request; it saves a WebP image. See the ScreenshotNeo documentation for PDF capture settings.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
  • Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; responses identify the page verdict and billing status in headers.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.
  • The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can PhantomJS render the page in landscape orientation?

Yes. Set orientation: 'landscape' in page.paperSize before rendering.

Does the PhantomJS render quality setting control PDF quality?

No. The documented quality argument is for JPEG and PNG output; PDF layout is controlled through paper size and margins.

Quick Recap

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.