Node.js jsPDF Tutorial to Convert SVG to PDF Document Using svg2pdf.js Module in Javascript Full Project For Beginners

 

 

Welcome folks today in this blog post we will be converting svg to pdf documents in node.js using svg2pdf.js library in javascript. All the full source code of the application is shown below.

 

 

 

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

 

 

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>

Leave a Reply