Svg2pdf

A JavaScript-only SVG to PDF conversion utility that runs in the browser.
GitHub
672
Created 9 years ago, last commit 2 months ago
22 contributors
449 commits
Stars added on GitHub, month by month
12
1
2
3
4
5
6
7
8
9
10
11
2023
2024
Stars added on GitHub, per day, on average
Yesterday
=
Last week
0.0
/day
Last month
+0.2
/day
Last 12 months
+0.2
/day
npmPackage on NPM
Monthly downloads on NPM
12
1
2
3
4
5
6
7
8
9
10
11
2023
2024
README

svg2pdf.js

NPM version Continous Integration for svg2pdf.js

A javascript-only SVG to PDF conversion utility that runs in the browser leveraging jsPDF.

Give it a trial: online playground.

Installation

You can get svg2pf.js via npm:

npm install svg2pdf.js jspdf --save
# or
yarn add svg2pdf.js jspdf

Since version 2.x, this repository no longer depends on a forked jsPDF but can be used with original MrRio/jsPDF.

If you want to use a development version from the repository, pay attention to the fact that the files in dist may reflect the last release version. So a simple package.json dependency link to the branch or revision will fail. See #102 for details.

Usage

import { jsPDF } from 'jspdf'
import 'svg2pdf.js'

const doc = new jsPDF()

const element = document.getElementById('svg')
doc
  .svg(element, {
    x,
    y,
    width,
    height
  })
  .then(() => {
    // save the created pdf
    doc.save('myPDF.pdf')
  })

Have a look at the typings file for detailed documentation.

Other module formats

Importing is also possible via requirejs:

require.config({
  baseUrl: './node_modules'
});
require([
  'svg2pdf.js/dist/svg2pdf.umd.min',
  'jspdf/dist/jspdf.umd.min'
], (svg2pdf, jsPDF) => {...});

or script-tag:

<script src="[node_modules|bower_components]/jspdf/dist/jspdf.umd.min.js"></script>
<script src="[node_modules|bower_components]/svg2pdf.js/dist/svg2pdf.umd.min.js"></script>

Concerning custom fonts and non US-ASCII characters

If you want to use other than really basic fonts and characters you have to add them first before calling svg2pdf:

Please refer to the jsPDF readme.

Reporting issues

Svg2pdf is by no means perfect. If you find something is not working as expected we are glad to receive an issue report from you. In order to be able to react efficiently we ask you to provide us with the necessary information.

Please stick to our Code of Conduct.

Building

If you want to play with the sources or build the minified js file yourself, check out the repository and use the npm scripts defined in package.json:

npm run build

Testing

The test folder contains a set of unit tests. Each unit test has its own folder and contains exactly two files:

  • A spec.svg file that contains the svg to test
  • A reference.pdf file that is generated automatically and serves as reference for regression testing

You can run the tests using

npm run createreferences && npm run test-unit

The tests use the Karma framework and run in a captured (headless) browser.

The createreferences script starts a server that automatically saves reference PDFs if they don't already exist. You can omit this command if you just want to test for regression.

If you're debugging and want to have visual feedback, you should switch the debug flag to true in test/unit/all.spec.js. This ensures that a new reference PDF will be created on every run. You might also want to disable some tests in the test/common/tests array.

Some tests might fail on your local machine, because of differences in text measuring. If you're contributing to a pull request and need to update the reference files, download the respective artifact from the CI build job on GitHub and check it in in a subsequent commit.

Dependencies

License

The MIT License (MIT)

Copyright (c) 2015-2023 yWorks GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.