Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SVG Support #6

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ef730e8
Build docker image
osterman Feb 17, 2024
6c54cb5
chore: update example image
actions-user Feb 17, 2024
c5364f0
fix typo
osterman Feb 17, 2024
e3ffec6
produce a step summary
osterman Feb 17, 2024
3f88b26
produce a step summary
osterman Feb 17, 2024
4c0dcfb
produce a step summary
osterman Feb 17, 2024
a25bfcd
produce a step summary
osterman Feb 17, 2024
592fcec
produce a step summary
osterman Feb 17, 2024
2fc56c7
produce a step summary
osterman Feb 17, 2024
d65152e
produce a step summary
osterman Feb 17, 2024
e4a8032
use docker image
osterman Feb 17, 2024
8df8b6e
use docker image
osterman Feb 17, 2024
fc96c88
use docker image
osterman Feb 17, 2024
cfc7c63
use docker image
osterman Feb 17, 2024
46f4cac
use docker image
osterman Feb 17, 2024
9d1d4e8
use docker image
osterman Feb 17, 2024
523766e
use docker image
osterman Feb 17, 2024
15e0474
use docker image
osterman Feb 18, 2024
e41da4b
use docker image
osterman Feb 18, 2024
3264063
use docker image
osterman Feb 18, 2024
ac53efe
use bump versions
osterman Feb 18, 2024
df2041a
rebuild readme
osterman Feb 18, 2024
4eac08f
Bump
osterman Feb 18, 2024
8da37aa
update readme and defaults
osterman Feb 18, 2024
25c5788
Update reamde
osterman Feb 18, 2024
3e92771
add svg conversion
osterman Feb 19, 2024
093eb82
fix javascript
osterman Feb 19, 2024
3be6764
add missing import
osterman Feb 19, 2024
c61080e
use correct output method
osterman Feb 19, 2024
8386675
fix pdf generation
osterman Feb 19, 2024
ad7baa3
fix pdf generation
osterman Feb 19, 2024
4612f1f
handle jpeg or jpg
osterman Feb 19, 2024
89b0916
Try uses with image
osterman May 18, 2024
f624c7f
Revert to docker action
osterman May 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build and Push Docker Image

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Build
id: build
uses: cloudposse/github-action-docker-build-push@main
with:
registry: ghcr.io
organization: "${{ github.event.repository.owner.login }}"
repository: "${{ github.event.repository.name }}"
login: "${{ github.actor }}"
password: "${{ secrets.GITHUB_TOKEN }}"
platforms: linux/amd64,linux/arm64

- name: Verify Image
run: |
docker pull ${{ steps.build.outputs.image }}:${{ steps.build.outputs.tag}}

- name: Job Summary
run: |
echo "## Docker Image Summary" >> $GITHUB_STEP_SUMMARY
echo '```json' >> $GITHUB_STEP_SUMMARY
docker inspect ${{ steps.build.outputs.image }}:${{ steps.build.outputs.tag}} >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
9 changes: 7 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:
types: [opened, synchronize, reopened]

permissions:
contents: write
packages: read

jobs:
screenshot:
runs-on: ubuntu-latest
Expand All @@ -17,7 +21,7 @@ jobs:
contents: write

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Run this composite action
id: screenshot
Expand All @@ -35,8 +39,9 @@ jobs:
"#name": "${{ github.event.repository.name }} 📷"
viewportWidth: 2000
viewportHeight: 800
token: ${{ secrets.GITHUB_TOKEN }}

- uses: stefanzweifel/git-auto-commit-action@v4
- uses: stefanzweifel/git-auto-commit-action@v5
name: Commit artifact
id: auto-commit
env:
Expand Down
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM ghcr.io/puppeteer/puppeteer:21.7.0

USER root

RUN apt-get update && apt-get install -y pdf2svg

# Install Additional Fonts
ARG TWITTER_COLOR_EMOJI_VERSION="14.0.2"
ARG GOOGLE_NOTO_COLOR_EMOJI_VERSION="2.042"

RUN mkdir -p /usr/share/fonts

# Solution found in https://stackoverflow.com/questions/50662388/running-headless-chrome-puppeteer-with-no-sandbox

# Install Noto Color Emoji
RUN curl --location --silent --show-error -o /usr/share/fonts/NotoColorEmoji.ttf https://github.com/googlefonts/noto-emoji/blob/v${GOOGLE_NOTO_COLOR_EMOJI_VERSION}/fonts/NotoColorEmoji.ttf?raw=true

# Install Twitter Emoji Font
RUN wget https://github.com/13rac1/twemoji-color-font/releases/download/v${TWITTER_COLOR_EMOJI_VERSION}/TwitterColorEmoji-SVGinOT-Linux-${TWITTER_COLOR_EMOJI_VERSION}.tar.gz && \
tar -xzvf TwitterColorEmoji-*.tar.gz --wildcards '*.ttf' && \
rm -f TwitterColorEmoji-*.tar.gz && \
find . -type f -wholename 'TwitterColorEmoji-*/*.ttf' -exec mv {} /usr/share/fonts \;

# Rebuild the font cache
RUN fc-cache -f -v && \
fc-list | grep -i emoji

USER pptruser
124 changes: 60 additions & 64 deletions README.md

Large diffs are not rendered by default.

37 changes: 27 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'Example composite GitHub action'
name: 'Screenshot'
description: "Generate a browser screenshot using puppeteer of any URL, including local files. Supports custom CSS overrides and Jquery style rewrites of content."
author: [email protected]
branding:
Expand All @@ -23,7 +23,7 @@ inputs:
default: png
omitBackground:
description: 'Omit the browser default background. Enable to support transparency.'
default: true
default: 'true'
viewportWidth:
description: 'Viewport width in pixels'
required: true
Expand All @@ -34,21 +34,26 @@ inputs:
description: 'Quality of the output image (1-100, applicable for JPEG)'
required: false
consoleOutputEnabled:
descripion: 'Whether or not to output the browser console log'
default: true
description: 'Whether or not to output the browser console log'
default: 'true'
deviceScaleFactor:
description: 'Specifies the device scale factor (pixel ratio) for the web page rendering. It determines how many physical pixels are used to represent a single logical pixel. For example, a device scale factor of 2 means one logical pixel is represented by two physical pixels, commonly used for high-DPI (Retina) displays. A value of 1 uses standard pixel density. This factor affects the resolution and quality of the rendered page or screenshot.'
default: '2'
required: false
fullPage:
description: 'Screen capture the entire page by scrolling down'
default: false
default: 'false'
waitForTimeout:
description: 'Number of miliseconds to delay before taking screenshot'
default: 500
description: 'Number of milliseconds to delay before taking screenshot'
default: '500'
puppeteerImage:
description: 'Docker image to run puppeteer. See https://github.com/puppeteer/puppeteer/pkgs/container/puppeteer'
default: 'ghcr.io/puppeteer/puppeteer:21.7.0'
default: 'ghcr.io/cloudposse-github-actions/screenshot:sha-65d10e40b3507a6d752bd0be8a9647f681b46f75'
token:
description: 'A GitHub token'
required: false
default: ""

outputs:
file:
description: "File containing the generated screenshot"
Expand Down Expand Up @@ -104,6 +109,19 @@ runs:
else
echo "DOCKER_VOLUMES=-v ${{ github.workspace }}:${{ github.workspace }}:rw -v ${{ github.action_path }}:${{ github.action_path }}:rw" >> "$GITHUB_ENV"
fi

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: inputs.token != ''
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ inputs.token }}

- name: Docker pull
shell: bash
run: |
docker pull ${{ inputs.puppeteerImage }}

- name: Run puppeteer to take screenshot
id: screenshot
Expand All @@ -118,5 +136,4 @@ runs:
- id: context
shell: bash
run: |
echo "file=${{ inputs.output }}" >> $GITHUB_OUTPUT

echo "file=${{ inputs.output }}" >> $GITHUB_OUTPUT
Binary file modified docs/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

| Name | Description | Default | Required |
|------|-------------|---------|----------|
| consoleOutputEnabled | consoleOutputEnabled | true | false |
| consoleOutputEnabled | Whether or not to output the browser console log | true | false |
| css | Custom CSS overrides | N/A | false |
| customizations | String representation of a YAML or JSON map of CSS paths (key) and replacement (value) | N/A | false |
| deviceScaleFactor | Specifies the device scale factor (pixel ratio) for the web page rendering. It determines how many physical pixels are used to represent a single logical pixel. For example, a device scale factor of 2 means one logical pixel is represented by two physical pixels, commonly used for high-DPI (Retina) displays. A value of 1 uses standard pixel density. This factor affects the resolution and quality of the rendered page or screenshot. | 2 | false |
Expand All @@ -13,10 +13,12 @@
| omitBackground | Omit the browser default background. Enable to support transparency. | true | false |
| output | Output image file path | screenshot.png | false |
| outputType | Output image type | png | false |
| puppeteerImage | Docker image to run puppeteer. See https://github.com/puppeteer/puppeteer/pkgs/container/puppeteer | ghcr.io/cloudposse-github-actions/screenshot:sha-65d10e40b3507a6d752bd0be8a9647f681b46f75 | false |
| token | A GitHub token | | false |
| url | URL of the HTML content to convert to an image. Use file:// for local files | N/A | true |
| viewportHeight | Viewport height in pixels | N/A | true |
| viewportWidth | Viewport width in pixels | N/A | true |
| waitForTimeout | Number of miliseconds to delay before taking screenshot | 500 | false |
| waitForTimeout | Number of milliseconds to delay before taking screenshot | 500 | false |


## Outputs
Expand Down
22 changes: 1 addition & 21 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
#!/bin/bash
set -ex

# Install Additional Fonts
TWITTER_COLOR_EMOJI_VERSION="14.0.2"
GOOGLE_NOTO_COLOR_EMOJI_VERSION="2.042"

mkdir -p ~/.fonts

# Solution found in https://stackoverflow.com/questions/50662388/running-headless-chrome-puppeteer-with-no-sandbox

# Install Noto Color Emoji
curl --location --silent --show-error -o ~/.fonts/NotoColorEmoji.ttf https://github.com/googlefonts/noto-emoji/blob/v${GOOGLE_NOTO_COLOR_EMOJI_VERSION}/fonts/NotoColorEmoji.ttf?raw=true

# Install Twitter Emoji Font
wget https://github.com/13rac1/twemoji-color-font/releases/download/v${TWITTER_COLOR_EMOJI_VERSION}/TwitterColorEmoji-SVGinOT-Linux-${TWITTER_COLOR_EMOJI_VERSION}.tar.gz
tar -xzvf TwitterColorEmoji-*.tar.gz --wildcards '*.ttf'
find . -type f -wholename 'TwitterColorEmoji-*/*.ttf' -exec mv {} ~/.fonts/ \;

# Rebuild the font cache
fc-cache -f -v
fc-list | grep -i emoji

# Install dependencies
npm install --no-fund option

# Generate screenshot
node main.js
node main.js
31 changes: 28 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const puppeteer = require('puppeteer');
const path = require('path');
const fs = require('fs').promises; // Import the fs module
const fsSync = require('fs');
const yaml = require('js-yaml');
const util = require('util');
const exec = util.promisify(require('child_process').exec);

const GITHUB_WORKSPACE = process.env.GITHUB_WORKSPACE || '.';
const INPUT_OUTPUT = process.env.INPUT_OUTPUT || 'docs/example.png';
Expand Down Expand Up @@ -29,6 +32,17 @@ async function readYamlFile(filePath) {
}
}

async function convertPdfToSvg(inputFile, outputFile) {
try {
const { stdout, stderr } = await exec(`pdf2svg ${inputFile} ${outputFile}`);
if(stdout)
console.log('stdout:', stdout);
if(stderr)
console.error('stderr:', stderr);
} catch (error) {
console.error('exec error:', error);
}
}

(async () => {
const browser = await puppeteer.launch({headless: 'new', dumpio: false});
Expand Down Expand Up @@ -89,11 +103,22 @@ async function readYamlFile(filePath) {
}, elementPaths);

await page.waitForTimeout(2000);

}
if (INPUT_OUTPUT_TYPE == "jpeg") {

if (INPUT_OUTPUT_TYPE == "svg") {
const baseExt = path.extname(INPUT_OUTPUT);
const baseName = path.basename(INPUT_OUTPUT, baseExt);
const pdfFile = `${baseName}.pdf`;
// First generate a PDF
await page.pdf({path: pdfFile, width: INPUT_VIEWPORT_WIDTH + 'px', height: INPUT_VIEWPORT_HEIGHT + 'px', printBackground: !INPUT_OMIT_BACKGROUND});

// Then convert the PDF to SVG
convertPdfToSvg(pdfFile, INPUT_OUTPUT);
} else if (INPUT_OUTPUT_TYPE == "pdf") {
const pdfFile = await page.pdf({path: INPUT_OUTPUT, width: INPUT_VIEWPORT_WIDTH + 'px', height: INPUT_VIEWPORT_HEIGHT + 'px', printBackground: !INPUT_OMIT_BACKGROUND});
} else if (INPUT_OUTPUT_TYPE == "jpeg" || INPUT_OUTPUT_TYPE == "jpg") {
// Quality parameter is only valid for JPEG images
await page.screenshot({path: INPUT_OUTPUT, 'quality': INPUT_IMAGE_QUALITY, 'type': INPUT_OUTPUT_TYPE, fullPage: INPUT_FULL_PAGE, omitBackground: INPUT_OMIT_BACKGROUND});
await page.screenshot({path: INPUT_OUTPUT, 'quality': INPUT_IMAGE_QUALITY, 'type': 'jpeg', fullPage: INPUT_FULL_PAGE, omitBackground: INPUT_OMIT_BACKGROUND});
} else {
await page.screenshot({path: INPUT_OUTPUT, 'type': INPUT_OUTPUT_TYPE, fullPage: INPUT_FULL_PAGE, omitBackground: INPUT_OMIT_BACKGROUND});
}
Expand Down