URL to PNG Convertor avatar
URL to PNG Convertor
Try for free

No credit card required

View all Actors
URL to PNG Convertor

URL to PNG Convertor

matthewgall/url-to-png
Try for free

No credit card required

Dockerfile

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

main.js

1const Apify = require('apify');
2
3Apify.main(async () => {
4    const input = await Apify.getValue('INPUT');
5    
6    if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
7    
8    console.log('Launching Puppeteer...');
9    const browser = await Apify.launchPuppeteer();
10    
11    console.log(`Opening URL: ${input.url}`);  
12    const page = await browser.newPage();
13    await page.goto(input.url);
14    
15    // Get the "viewport" of the page, as reported by the page.
16    console.log('Determining page dimensions...');
17    const dimensions = await page.evaluate(() => ({
18        width: document.documentElement.clientWidth,
19        height: document.documentElement.clientHeight,
20        deviceScaleFactor: window.devicePixelRatio
21    }));
22    console.log(`Dimension: ${JSON.stringify(dimensions)}`);
23    
24    // Grab a screenshot
25    console.log('Saving screenshot...');
26    const screenshotBuffer = await page.screenshot();
27    await Apify.setValue('screenshot.png', screenshotBuffer, { contentType: 'image/png' });
28    
29    console.log('Closing Puppeteer...');
30    await browser.close();
31    
32    console.log('Done.');
33    console.log('You can check the output in the key-value on the following URLs:');
34    const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
35    console.log(`- https://api.apify.com/v2/key-value-stores/${storeId}/records/screenshot.png`);
36});

package.json

1{
2    "name": "apify-project",
3    "version": "1.3.4",
4    "description": "",
5    "author": "It's not you it's me",
6    "license": "ISC",
7    "dependencies": {
8        "apify": "latest"
9    },
10    "scripts": {
11        "start": "node main.js"
12    }
13}
Developer
Maintained by Community
Actor metrics
  • 2 monthly users
  • 53.8% runs succeeded
  • 0.0 days response time
  • Created in Nov 2017
  • Modified over 1 year ago
Categories