svg-to-pdf

1.4.4 • Public • Published

svg-to-pdf

pre-press oriented node.js module to transform a SVG string into printable PDF file




Features

  • 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 its color=green attribute to its children)
  • All PDFKit vector 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

Limited support

  • viewBox attribute is parsed only on the <svg> root element, so all system coordinates must be absolute
  • The preserveAspectRatio attribute 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-baseline
    • font-family
    • font-size
    • letter-spacing
    • text-anchor
    • word-spacing
  • % unit should work as intended, but some bugs can occur when working in complex coordinates systems (like viewports deep nest)

Not supported, won't be supported

CSS styling

Use svgo beforehand to apply CSS styling as element attributes:

full: false
plugins:
  - inlineStyles:
      useMqs:
        - ''
        - screen
        - print
  - removeStyleElement
  - converStyleToAttrs

NOTE: see svgo guide to read more about svgo config.

Installation

$ npm install --save svg-to-pdf

Usage

const 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.

Development

$ 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

Credits

  • SVG initial parsing is done thanks to svg-parser.
  • PDF file is generated with PDFKit.

Related

License

MIT.

Readme

Keywords

none

Package Sidebar

Install

npm i svg-to-pdf

Weekly Downloads

5

Version

1.4.4

License

MIT

Unpacked Size

78.5 kB

Total Files

42

Last publish

Collaborators

  • arnaudjuracek