Embedding SVG in PDF (exporting SVG to PDF using JS)

70,220

Solution 1

For anyone looking for a JS-only solution: PDFKit seems to be the superior solution to generate PDF from JS these days, and it supports all SVG geometry primitives (including interpreting path geometry strings) out of the box. All that would be needed to render existing SVG content would be a DOM-walker that keeps track of CSS styling and inheritance, if you do not require complex stuff like symbols etc.

I wasn't successful with the sketchy SVG support of the jsPDF/svgToPdf combo mentioned in the other answer, and the source code of these two didn't look very well-crafted and complete to me.

Edit: Usage example JSFiddle

Solution 2

I'll reply to my own question. I ended up using DocRaptor that can be called client-side from JavaScript. This technically solves my problem, even though it is a non-free solution. If I the answer could be a server-side-solution, I could use wkhtmltopdf as proposed by Mic.

Solution 3

jsPDF has a plugin for that: svgToPdf:

https://github.com/ahwolf/jsPDF/blob/master/jspdf.plugin.svgToPdf.js

I haven't tried it, but this could allow discarding the use of an external API and/or having to rely on a server-side solution.

Solution 4

Did you try WKHTMLTOPDF? It's a free tool based on webkit.
We wrote a small tutorial here.

On a Mac, with Safari or Chrome, you can save an HTML page with embedded SVG to a PDF.
Since these browsers use WKHTMLTOPDF internally, may be this will work for you as well.

Share:
70,220
Simeon
Author by

Simeon

Updated on April 09, 2021

Comments

  • Simeon
    Simeon about 3 years

    The starting points: I don't have a server that can provide anything but static files. And I have an SVG element (dynamically created) in my <body> that I want to export to a vector format, preferrably PDF or SVG.

    I started looking at using the already existing lib jsPDF along with downloadify. It worked fine. Unfortunately, this does not support SVG, only text.

    I've read about the PDF format's possiblities to embed SVG images, and it seems to have been enabled since Acrobat Reader 5 (along with the ImageViewer plugin). But it doesn't work. I've tried with 3 different PDF readers without success.

    Does this mean that PDFs has dropped SVG embedding support? I haven't found anything on this.

    I have two questions; can this be solved? And if yes, what are the specifications for embedding SVG inside of a PDF? With that info, I can build that support in jsPDF myself.

    The browser support demands are Safari, Chrome and Firefox. The versions that supports SVG.

  • Simeon
    Simeon about 13 years
    I'm asking for a client-side solution here. However, you are right in that I've not specified what browser support I'm going for. So I've now edited my answer to include that
  • Mic
    Mic about 13 years
    Thanks for the link to DocRaptor. If you go the route of hosting something, you can use WKHTMLTOPDF, as it is free and open sourced
  • Simeon
    Simeon about 13 years
    Added your answer and removed Prince XML as an alternative, thanks!
  • art-solopov
    art-solopov almost 9 years
    I, unfortunately, still can't make wkhtmltopdf work. It only produces blank pages for me.
  • Mic
    Mic almost 9 years
    @art-solopov, times have changed, as mentionned above, you could switch to pdfkit: pdfkit.org/docs/vector.html
  • BelgoCanadian
    BelgoCanadian over 6 years
    I'm currently trying this approach but I only seem to get a blank pdf from it. Do you know of any examples out there that use this plugin?
  • Arun Kandregula
    Arun Kandregula about 6 years
    One question. 1. Open pdfkit.org/demo/browser.html. 2. Open Chrome Inspect Window. 3. Click "Toggle Device Toolbar" icon on the top menu. 4. Refresh the page. 5. I CANNOT see the pdf on the right window in the mobile view. 6. Do anyone know why the mobile view is not rendering the PDF. ? Is it this PDFKit specific or general problem with opening pdf on mobile ?
  • Arpit Gupta
    Arpit Gupta over 5 years
    EVO pdf doesn't fit the svg image on entire pdf page. There is lot of blank space on all the sides of the image. I have tried all the option like FitWidth, FitHeight, StretchToFit etc. But couldn't solve the issue.
  • James McLaughlin
    James McLaughlin about 5 years
    PDFKit in conjunction with github.com/alafr/SVG-to-PDFKit seems to be the way to go.
  • Jameson
    Jameson almost 5 years
    I'm experiencing the same issue as ArpitGupta where the layout around the SVG is completely wrong when using @EvoPdf I have a container div wrapping an SVG and I'm using flexbox to vertically & horizontally center the container. When converted to PDF, the layout/alignment is the same as if I completely remove the flexbox styles.
  • Mustkeem K
    Mustkeem K over 4 years
    cant convert html to pdf directly
  • Shmack
    Shmack over 3 years
    It doesn't support the image tag!?
  • Frank
    Frank about 2 years
    shapes are correct but all color and text is blacked out.
  • Ulf Aslak
    Ulf Aslak about 2 years
    @Frank hmmm.. can't reproduce. What browser and version? Anyone else getting this?
  • Frank
    Frank about 2 years
    I created a minimal example here and linked to your answer in the post stackoverflow.com/questions/72061301/…