A javascript-only SVG to PDF conversion utility that runs in the browser leveraging jsPDF
npm install @leoko/svg2pdf.jssh
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
`js
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.
$3
Importing is also possible via requirejs:
`javascript
require.config({
baseUrl: './node_modules'
});
require([
'svg2pdf.js/dist/svg2pdf.umd.min',
'jspdf/dist/jspdf.umd.min'
], (svg2pdf, jsPDF) => {...});
`
or script-tag:
`html
`
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:
`bash
npm run build
`
$3
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
`sh
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.