pre-press oriented node.js module to transform a SVG string into printable PDF file
- Support for this SVG elements:
<defs><symbol><clipPath><use><g><path><rect><circle><ellipse><line><image><polyline><polygon><image><text>
- Group inheritance (ie:
<g color=green>will pass itscolor=greenattribute to its children) - All
PDFKitvector graphics supported operations - PDF file permissions and limitations w/ password support
- PDF file metadatas edition (author, subject, keywords, etc…)
- Length units support and implicit conversion to PDF points
- Support for uncalibrated CMYK colors using
device-cmyk(), as described in the SVG 2 Working draft 11 - Pre-press inspired pre-flight process, which gives insight and warnings after generating a PDF file
- Pre-press related features such as bleed area and printers marks:
- crop marks
- colors marks
- registration marks
viewBoxattribute is parsed only on the<svg>root element, so all system coordinates must be absolute- The
preserveAspectRatioattribute is fully supported, but only on the<image>element <text>elements are rendered only if they do not have complex structure (such as nested element)- Supported
<text>specific presentation attributes are:dominant-baselinefont-familyfont-sizeletter-spacingtext-anchorword-spacing
%unit should work as intended, but some bugs can occur when working in complex coordinates systems (like viewports deep nest)
Use svgo beforehand to apply CSS styling as element attributes:
full: false
plugins:
- inlineStyles:
useMqs:
- ''
- screen
- print
- removeStyleElement
- converStyleToAttrsNOTE: see svgo guide to read more about svgo config.
$ npm install --save svg-to-pdfconst SVG2PDF = require('svg-to-pdf')
const options = {
docPath: '/tmp/file.pdf',
// All attributes refering to an external file path will be resolved from
// this root path
rootPath: process.cwd(),
// color space can be either 'rgb' or 'cmyk'
colorSpace: 'rgb',
// SVG specification specifies that all unitless lengths are expressed in a
// so-called "user unit", usually defaulting to pixel
userUnit: 'px', // 'px', 'mm', 'cm', 'pt', 'in'…
// Pre-press related features
bleed: 0, // '10mm', '100px', 10 <userUnit>
marks: {
colors: true,
crop: true,
registration: true
},
// Attach an additional [...files] array to the output, providing further
// insights on PDF generation.
// Be warned that files analysis can be perf heavy.
createFilesReport = true,
// PDF file options, see PDFKit
pdfVersion: 1.3,
userPassword: undefined,
ownerPassword: undefined,
permissions: undefined,
title: '',
subject: '',
author: '',
producer: 'arnaudjuracek/svg-to-pdf@semver',
keywords: ''
}
// using Promises
SVG2PDF(svgString, options)
.then(({ warnings, files }) => console.log(warnings))
.catch(error => console.error(error))
// using async/await
;(async () => {
const { warnings, files } = await SVG2PDF(svgString, options)
console.log(warnings)
})()Note: for PDF file related options, see PDFKit options.
$ npm test # run svg-to-pdf against test/sample.svg
$ npm test -- --input=path/to/file.svg --output=path/to/output.svg --color-space=rgb|cmyk- SVG initial parsing is done thanks to
svg-parser. - PDF file is generated with
PDFKit.