2.4 KiB
Absolutely! Here’s a clear, step-by-step README for your Puppeteer SVG-to-PDF script using local installation of Puppeteer.
SVG to PDF Converter (Node.js + Puppeteer)
This script converts SVG files (including those with <foreignObject>) to PDFs using Node.js and Puppeteer. The PDF will match the SVG’s width and height or viewBox.
Prerequisites
-
Node.js v14+ (tested on v20.x)
-
npm (comes with Node.js)
-
Windows, macOS, or Linux
Installation
- Clone or copy the project folder to your machine.
Example structure:
convert_svg_to_pdf_with_chrome/ │ svg_to_pdf.js
- Open a terminal in the project folder:
bash
cd path\to\convert_svg_to_pdf_with_chrome
- Initialize npm (if not already done):
bash
npm init -y
- Install Puppeteer locally:
bash
npm install puppeteer
This downloads Puppeteer and its bundled Chromium inside the project folder.
- Verify Puppeteer is installed:
bash
npm list puppeteer
You should see something like:
graphql
convert_svg_to_pdf_with_chrome@1.0.0 └── puppeteer@24.x.x
Usage
Convert a single SVG
bash
node svg_to_pdf.js "path\to\file.svg"
-
The PDF will be saved in the same folder as the SVG with the same filename.
-
Example:
bash
node svg_to_pdf.js "D:\Dropbox\Gitea_OD\Utility_Apps\Bat\convert_svg_to_pdf_with_chrome\test.svg"
- Output:
makefile
D:\Dropbox\Gitea_OD\Utility_Apps\Bat\convert_svg_to_pdf_with_chrome\test.pdf
Convert SVG to PDF with custom output filename
bash
node svg_to_pdf.js "input.svg" "output.pdf"
- Example:
bash
node svg_to_pdf.js test.svg
node svg_to_pdf.js test.svg test.pdf
Notes
-
The script automatically detects the SVG’s
widthandheightattributes or itsviewBoxto set the PDF size. -
Fully supports
<foreignObject>and embedded HTML/CSS. -
Works on Windows, macOS, and Linux as long as Node.js and Puppeteer are installed locally.
Troubleshooting
- Error: Cannot find module 'puppeteer'
Make sure you installed Puppeteer locally in the same folder:
bash
npm install puppeteer
- Chromium download issues
Puppeteer automatically downloads Chromium. Ensure your network allows downloads. If needed, set thePUPPETEER_SKIP_CHROMIUM_DOWNLOADorPUPPETEER_EXECUTABLE_PATHenvironment variable for a custom browser.