npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-to-pdf

v1.4.4

Published

transform SVG string to printable PDF

Downloads

9

Readme

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.