PDF Generator avatar
PDF Generator

Deprecated

Pricing

Pay per usage

Go to Store
PDF Generator

PDF Generator

Deprecated

Developed by

Yinka Olukorode

Yinka Olukorode

Maintained by Community

A fork of the url-to-pdf actor with added name input and delay until the network is idle. Opens a web page in headless Chrome using Puppeteer and prints the output to a PDF file.

0.0 (0)

Pricing

Pay per usage

3

Total users

38

Monthly users

1

Last modified

3 years 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:beta
# 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(input.name, 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});