HTML to PDF Converter avatar
HTML to PDF Converter

Pricing

Pay per usage

Go to Store
HTML to PDF Converter

HTML to PDF Converter

Developed by

Jan Čurn

Jan Čurn

Maintained by Community

Loads a web page in headless Chrome using Puppeteer and prints it to PDF. The input is a JSON object and output is a PDF file.

0.0 (0)

Pricing

Pay per usage

10

Total users

443

Monthly users

11

Runs succeeded

88%

Last modified

7 months ago

Dockerfile

# This is a template for a Dockerfile used to run acts in Actor system.
# The base image name below is set during the act build, based on user settings.
# IMPORTANT: The base image must set a correct working directory, such as /usr/src/app or /home/user
FROM apify/actor-node-puppeteer
# Second, copy just package.json and package-lock.json since it should be
# the only file that affects "npm install" in the next step, to speed up the build
COPY package*.json ./
# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging too much and print the dependency
# tree for debugging
RUN npm --quiet set progress=false \
&& npm install --only=prod --no-optional \
&& echo "Installed NPM packages:" \
&& (npm list --all || true) \
&& echo "Node.js version:" \
&& node --version \
&& echo "NPM version:" \
&& npm --version
# Copy source code to container
# Do this in the last step, to have fast build if only the source code changed
COPY --chown=node:node . ./
# NOTE: The CMD is already defined by the base image.
# Uncomment this for local node inspector debugging:
# CMD [ "node", "--inspect=0.0.0.0:9229", "main.js" ]

package.json

{
"name": "apify-project",
"version": "0.0.1",
"description": "",
"author": "It's not you it's me",
"license": "ISC",
"dependencies": {
"apify": "latest"
},
"scripts": {
"start": "node main.js"
}
}

main.js

1const Apify = require('apify');
2
3Apify.main(async () => {
4 console.log('Fetching input...');
5 const input = await Apify.getValue('INPUT');
6 if (!input || typeof(input.url) !== 'string') {
7 throw new Error('Input must be an object with the "url" property');
8 }
9
10 console.log('Launching headless Chrome...');
11 const browser = await Apify.launchPuppeteer();
12 const page = await browser.newPage();
13
14 console.log(`Loading page (url: ${input.url})...`);
15 await page.goto(input.url);
16
17 if (input.sleepMillis > 0) {
18 console.log(`Sleeping ${input.sleepMillis} millis...`);
19 await new Promise((resolve) => setTimeout(resolve, input.sleepMillis));
20 }
21
22 const opts = input.pdfOptions || {};
23 delete opts.path; // Don't store to file
24 console.log(`Printing to PDF (options: ${JSON.stringify(opts)})...`);
25 const pdfBuffer = await page.pdf(opts);
26
27 console.log(`Saving PDF (size: ${pdfBuffer.length} bytes) to output...`);
28 await Apify.setValue('OUTPUT', pdfBuffer, { contentType: 'application/pdf' });
29
30 const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
31
32 // NOTE: Adding disableRedirect=1 param, because for some reason Chrome doesn't allow pasting URLs to PDF
33 // that redirect into the browser address bar (yeah, wtf...)
34 console.log('PDF file has been stored to:');
35 console.log(`https://api.apify.com/v2/key-value-stores/${storeId}/records/OUTPUT?disableRedirect=1`);
36});