Html Renderer avatar
Html Renderer

Pricing

Pay per usage

Go to Store
Html Renderer

Html Renderer

Developed by

Jakub Balada

Jakub Balada

Maintained by Community

Generate image for your HTML using a headless browser

0.0 (0)

Pricing

Pay per usage

1

Total users

23

Monthly users

1

Runs succeeded

67%

Last modified

2 years ago

Dockerfile

# Dockerfile contains instructions how to build a Docker image that
# will contain all the code and configuration needed to run your actor.
# For a full Dockerfile reference,
# see https://docs.docker.com/engine/reference/builder/
# First, specify the base Docker image. Apify provides the following
# base images for your convenience:
# apify/actor-node-basic (Node.js 10 on Alpine Linux, small and fast)
# apify/actor-node-chrome (Node.js 10 + Chrome on Debian)
# apify/actor-node-chrome-xvfb (Node.js 10 + Chrome + Xvfb on Debian)
# For more information, see https://apify.com/docs/actor#base-images
# Note that you can use any other image from Docker Hub.
FROM apify/actor-node-chrome
# Copy all files and directories with the source code
COPY . ./
# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging to 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 \
&& echo "Node.js version:" \
&& node --version \
&& echo "NPM version:" \
&& npm --version
# Specify how to run the source code
CMD npm start

INPUT_SCHEMA.json

{
"title": "HTML renderer Input",
"type": "object",
"description": "Use the following form to configure HTML renderer.",
"schemaVersion": 1,
"properties": {
"html": {
"title": "HTML to generate",
"type": "string",
"description": "HTML to be opened in a broswer and screenshotted",
"editor": "textarea",
"example": "<html>\n<head>\n<title>Page Title</title>\n</head>\n<body>\n\n<h1>This is a Heading</h1>\n<p>This is a paragraph.</p>\n\n</body>\n</html>"
},
"viewport": {
"title": "Viewport",
"type": "object",
"description": "Viewport of a screenshot",
"default": {"width": 800, "height":600},
"editor": "json"
},
"format": {
"title": "Output format",
"type": "string",
"description": "Output format (jpeg or png)",
"editor": "select",
"default": "jpeg",
"enum": ["jpeg", "png"]
},
"outputType": {
"title": "Output type",
"type": "string",
"description": "Output type (link to screenshot or image itself)",
"editor": "select",
"default": "link",
"enum": ["link", "image"]
}
},
"required": [
"html"
]
}

main.js

1const Apify = require('apify');
2
3Apify.main(async () => {
4
5 const input = await Apify.getInput();
6
7 const options = {};
8
9 if (input.viewport) {
10 options.defaultViewport = input.viewport
11 }
12 const browser = await Apify.launchPuppeteer(options);
13
14 const page = await browser.newPage();
15 await page.setContent(input.html);
16 const image = await page.screenshot({type: input.format, fullPage: true});
17
18 await Apify.setValue('screenshot', image, { contentType: `image/${input.format}` });
19 if (input.outputType == "link") {
20 const keyValueStore = await Apify.openKeyValueStore();
21 const link = keyValueStore.getPublicUrl('screenshot');
22 await Apify.setValue('OUTPUT', {link});
23 } else {
24 await Apify.setValue('OUTPUT', image, { contentType: `image/${input.format}` });
25 }
26});

package.json

{
"name": "my-actor",
"version": "0.0.1",
"dependencies": {
"apify": "^0.13.7"
},
"scripts": {
"start": "node main.js"
},
"author": "Me!"
}