105 lines
2.4 KiB
Markdown
105 lines
2.4 KiB
Markdown
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
|
||
|
||
1. **Clone or copy the project folder** to your machine.
|
||
Example structure:
|
||
|
||
|
||
|
||
`convert_svg_to_pdf_with_chrome/ │ svg_to_pdf.js`
|
||
|
||
2. **Open a terminal** in the project folder:
|
||
|
||
bash
|
||
`cd path\to\convert_svg_to_pdf_with_chrome`
|
||
|
||
3. **Initialize npm (if not already done):**
|
||
|
||
bash
|
||
`npm init -y`
|
||
|
||
4. **Install Puppeteer locally:**
|
||
|
||
bash
|
||
`npm install puppeteer`
|
||
|
||
> This downloads Puppeteer and its bundled Chromium inside the project folder.
|
||
|
||
5. 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 `width` and `height` attributes or its `viewBox` to 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 the `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` or `PUPPETEER_EXECUTABLE_PATH` environment variable for a custom browser.
|
||
|
||
|