UT

URL to PNG Convertor

  • matthewgall/url-to-png
  • Modified
  • Users 28
  • Runs 1.4k
  • Created by Author's avatarMatthew Gall

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-chrome:15-10.1.0

# 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" ]

main.js

const Apify = require('apify');

Apify.main(async () => {
    const input = await Apify.getValue('INPUT');
    
    if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
    
    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();
    
    console.log(`Opening URL: ${input.url}`);  
    const page = await browser.newPage();
    await page.goto(input.url);
    
    // Get the "viewport" of the page, as reported by the page.
    console.log('Determining page dimensions...');
    const dimensions = await page.evaluate(() => ({
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        deviceScaleFactor: window.devicePixelRatio
    }));
    console.log(`Dimension: ${JSON.stringify(dimensions)}`);
    
    // Grab a screenshot
    console.log('Saving screenshot...');
    const screenshotBuffer = await page.screenshot();
    await Apify.setValue('screenshot.png', screenshotBuffer, { contentType: 'image/png' });
    
    console.log('Closing Puppeteer...');
    await browser.close();
    
    console.log('Done.');
    console.log('You can check the output in the key-value on the following URLs:');
    const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
    console.log(`- https://api.apify.com/v2/key-value-stores/${storeId}/records/screenshot.png`);
});

package.json

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