Convert Webpage to PDF Online using PDF.co

Sep 2, 2024·10 Minutes Read

PDF.co platform provides a way to easily automate web pages to PDF and HTML to PDF generation. Output PDF files are high-quality and ready for printing and archiving. Moreover, you can split, merge, or protect PDFs.

Why Use Webpage to PDF and HTML to PDF Automation

Automatically Save Important Pages

Instead of manually saving web pages and links to PDF, you can automate this process using the PDF.co automation platform. It will automatically generate a PDF from a given link.

Keep Original Webpage Layout & Formatting

When you save a web page to PDF, you want to maintain the layout and format of the content as it was when you saw it. With our website to PDF Web API, you can save any website page you want with just two steps.

Fully Featured HTML to PDF Conversion

Fully featured HTML to PDF conversion (with styles, fonts, images, and drawings) is supported by the PDF.co platform. It includes CSS styles printing-ready full colors and high-quality images.

Web API Supports Multiple Languages

PDF.co platform can be used by software developers from programming languages such as PHP, Javascript, .NET and ASP.NET, C#, Java, Visual Basic, and many others. Find source code samples in our API documentation.

API and Business Automation Platforms

If you are not a developer, you can also easily automate your PDF operations via popular business automation platforms: ZapierMakeAirtableSalesforce, and more.

How to Use Webpage to PDF Web API

By checking this brief demo, you can see the program in action. That is how you convert any web page to PDF with the help of Webpage to PDF Web API.

A random Wikipedia page was used in this demo tutorial.

To make the program run, you should pick one of the programming languages we offer, for example, JavaScript. Below you can see the original web page to the final PDF file transformation.

Webpage
Webpage
PDF Output
PDF Output

Now before we look into the code, let me walk you through the PDF.co Web API endpoint documentation which we are going to use in our demo application.

Endpoint

  • URL: https://api.pdf.co/v1/pdf/convert/from/url
  • Method: POST

Headers

Key: x-api-key

Value: {{x-api-key}}

Description: Required. API key for PDF.co. Get yours at https://pdf.co/

Now after setting the above endpoint and headers, we need to pass/set the following parameters:

  • Url*: Link to input HTML file or website page to be converted.
  • margins (optional): Set to css style margins like 10px, 5mm, 5in for all sides or “5px 5px 5px 5px”
  • paperSize (optional): Can be Letter, A4, A5, A6 or custom size in pixels, mm or inches with width and height separated by space like this: “200 300”, “200px 300px”, “200mm 300mm”, “20cm 30cm”
  • orientation (optional): Set to Portrait or Landscape. Portrait by default.
  • printBackground (optional): true by default. Set to false to disable printing of background.
  • header (optional): Set to HTML for the header to be applied on every page at the header.
  • footer (optional): Set an HTML for the footer to be applied on every page at the bottom.

Check out the JavaScript source code that was used for this activity.

Javascript (Node.js) Code Snippet

var https = require("https");
var path = require("path");
var fs = require("fs");

// The authentication key (API Key).
const API_KEY = "***********************************";

// URL of web page to convert to PDF document.
const SourceUrl = "https://en.wikipedia.org/wiki/Style_sheet_(web_development)";
// Destination PDF file name
const DestinationFile = "./result.pdf";

// Prepare request to `Web Page to PDF` API endpoint
var queryPath = `/v1/pdf/convert/from/url`;
// JSON payload for api request
var jsonPayload = JSON.stringify({
    name: path.basename(DestinationFile), url: SourceUrl
});

var reqOptions = {
    host: "api.pdf.co",
    method: "POST",
    path: queryPath,
    headers: {
        "x-api-key": API_KEY,
        "Content-Type": "application/json",
        "Content-Length": Buffer.byteLength(jsonPayload, 'utf8')
    }
};
// Send request
var postRequest = https.request(reqOptions, (response) => {
    response.on("data", (d) => {
        // Parse JSON response
        var data = JSON.parse(d);       
        if (data.error == false) {
            // Download PDF file
            var file = fs.createWriteStream(DestinationFile);
            https.get(data.url, (response2) => {
                response2.pipe(file)
                .on("close", () => {
                    console.log(`Generated PDF file saved as "${DestinationFile}" file.`);
                });
            });
        }
        else {
            // Service reported error
            console.log(data.message);
        }
    });
}).on("error", (e) => {
    // Request error
    console.log(e);
});

// Write request data
postRequest.write(jsonPayload);
postRequest.end();

You can find C# source codes to convert Webpage to PDF here.

You can find Java source codes to convert Webpage to PDF here.

You can find Python source codes to convert Webpage to PDF here.

You can find JavaScript source codes to convert Webpage to PDF here.

You can find PHP source codes to convert Webpage to PDF here.

You can find VB.NET source codes to convert Webpage to PDF here.

You can find PowerShell source codes to convert Webpage to PDF here.

Frequently Asked Questions – Convert HTML to PDF

How Can I Save an Entire Web Page as a PDF?

You can save an entire Web page as a PDF by using our Webpage to PDF Web API. The API platform is designed to save large web pages fast, saving you valuable time. You will also be happy to know that the quality of the images on the pages will not be degraded, and the PDFs will be of minimal sizes and high quality.

How Do I Convert HTML to PDF

The API can help you convert any published HTML content to PDFs. Just use your API credits for this. The PDF files will be automatically generated from the HTML content you provide, and the API will do the HTML to PDF conversion for you.

For example, we recommend this API feature for those looking for ways to convert HTML tutorials to PDFs so that they can access them offline. The converted PDFs are also of small sizes, and therefore easily shareable. As an added functionality, you can choose to store the converted files in your Google Drive, or your Dropbox accounts, instead of downloading them. Otherwise, you can convert PDF files to other available formats.

Related Tutorials

See Related Tutorials