HTML to PDF

Develop HTML to PDF converter in Node.js.

Converting content or entire webpages to is a frequent requirement for businesses and developers. Whether for reporting, archiving, or sharing cleanly formatted documents, HTML to PDF ensures content consistency and accessibility across platforms.

In this article, we’ll show how to convert HTML to PDF in Node.js using Aspose.PDF Cloud SDK, a powerful REST API that streamlines document transformation.

🚀 Node.js HTML to PDF REST API

Aspose.PDF Cloud SDK for Node.js allows you to convert HTML files, zipped HTML content, or web URLs to high-quality PDF documents. The API supports setting page size, orientation, margins, and much more.

  1. Install the SDK:
npm install asposepdfcloud --save

Get your Client ID and Client Secret from the Aspose Cloud Dashboard. For related information, you may consider visiting quick start.

🛠️ Convert HTML or URL to PDF using Node.js

Use the following code to convert HTML content to PDF using Node.js:

  • Firstly, initialize the PdfApi with your credentials.
  • Secondly, specify the name of input HTML file and resultant PDF details.
  • Lastly, call the GetHtmlInStorageToPdf(…) API routine to perform HTML to PDF conversion.
HTML to PDF

HTML to PDF conversion preview.

💻 Convert HTML to PDF via cURL

Here’s how to perform the same conversion using cURL:

  1. Generate an access token:
curl -v "https://api.aspose.cloud/connect/token" \
 -X POST \
 -d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 -H "Accept: application/json"

Once the JWT token has been generated, please execute the following command to convert the HTML to PDF format:

curl -v "https://api.aspose.cloud/v3.0/pdf/{resultantFile}/create/html?srcPath={sourceHTML.zip}&htmlFileName={resultantHTML}" \
-X PUT \
-H  "accept: application/json" \
-H  "authorization: Bearer {accessToken}" \
-d {} -v

Replace sourceHTML.zip with the name of .zip archive containing HTML and related resources, and resultantHTML with the name of resultant PDF document to be stored in the cloud storage.

Execute the following command to convert HTML to PDF and save the resultant output on local drive.:

curl -v "https://api.aspose.cloud/v3.0/pdf/create/html?srcPath=sourceFolder.zip&htmlFileName=source.html&height=1000&width=800&isLandscape=false&marginLeft=10&marginBottom=10&marginRight=10&marginTop=10" \
-X GET \
-H "accept: multipart/form-data" \
-H "authorization: Bearer {accessToken}" \
-o "Converted.pdf"

The resultant PDF generated in above example can be downloaded from htmlOutput.pdf.

✅ Conclusion

Using Aspose.PDF Cloud SDK for Node.js, you can convert HTML, zipped HTML packages, or live URLs to polished PDF documents with ease. Whether through SDK or cURL, the process is simple and highly customizable.

We highly recommend visiting the following blog posts to learn more about: