Skip to content

Instantly share code, notes, and snippets.

@s417-lama
Created April 29, 2020 07:18
Show Gist options
  • Save s417-lama/84bf66de1096c4587e8187092fb41684 to your computer and use it in GitHub Desktop.
Save s417-lama/84bf66de1096c4587e8187092fb41684 to your computer and use it in GitHub Desktop.
Reliable way to convert an SVG file to a PDF file using headless Chrome
#!/bin/bash
#
# Convert an SVG file to a PDF file by using headless Chrome.
#
if [ $# -ne 2 ]; then
echo "Usage: ./svg2pdf.bash input.svg output.pdf" 1>&2
exit 1
fi
INPUT=$1
OUTPUT=$2
HTML="
<html>
<head>
<style>
body {
margin: 0;
}
</style>
<script>
function init() {
const element = document.getElementById('targetsvg');
const positionInfo = element.getBoundingClientRect();
const height = positionInfo.height;
const width = positionInfo.width;
const style = document.createElement('style');
style.innerHTML = \`@page {margin: 0; size: \${width}px \${height}px}\`;
document.head.appendChild(style);
}
window.onload = init;
</script>
</head>
<body>
<img id=\"targetsvg\" src=\"${INPUT}\">
</body>
</html>
"
tmpfile=$(mktemp XXXXXX.html)
trap "rm -f $tmpfile" EXIT
echo $HTML > $tmpfile
google-chrome --headless --disable-gpu --print-to-pdf=$OUTPUT $tmpfile
@guillermo
Copy link

Nice. It works really well Thanks.
I fork it to add support for files with spaces after going throw the same problem as this user: https://stackoverflow.com/questions/47790308/chrome-headless-in-ubuntu.

@asmeurer
Copy link

I added Mac support in my fork. https://gist.github.com/asmeurer/b9851296578743db7c0e985939a462d9. I think it will also support Chromium (if I understand correctly Chromium uses chrome instead of google-chrome, but I didn't verify this).

In case it helps anyone, I also noticed that in some cases, this produces a PDF with an extra blank page. This answer suggests adding line-height: unset; to the CSS, but I couldn't get that to work. I was able to fix it by editing the SVG file and making the width and height a little tighter. I don't know if the process can be automated.

Can I ask, is it OK to use this script in a BSD licensed project (I need something like this for sympy)?

@s417-lama
Copy link
Author

@asmeurer Thank you for your interest. As I released this script under public domain, you can use it freely without any acknowledgment.

Applied license:
https://gist.github.com/s417-lama/9ff410fdb3e2adead81f7ca035192f43

@s417-lama
Copy link
Author

As a note, I have switched from this script to rsvg-convert command (librsvg2) to convert svg files to pdf files, because Chromium also has some issues regarding svg conversion.

For example, pattern fill in svg is converted as a raster image rather than vectors in Chromium.

rsvg-convert can handle pattern fill in an appropriate way and is stable (at least for my usage), so I'm currently using it.

Example usage:

rsvg-convert -f pdf foo.svg -o foo.pdf

@asmeurer
Copy link

@asmeurer Thank you for your interest. As I released this script under public domain, you can use it freely without any acknowledgment.

Thanks.

As a note, I have switched from this script to rsvg-convert command (librsvg2) to convert svg files to pdf files, because Chromium also has some issues regarding svg conversion.

That's good to know. The svgs I need to convert for SymPy get messed up in some way or other with every tool I've tried (Inkscape, rsvg-convert, convert, cairosvg) except for Chrome.

@moritzmhmk
Copy link

For everyone looking for a solution to the annoying blank page being appended: when sizing is not super critical just add 1px to the height in line 29.
Looking forward to dominant-baseline support in librsvg2 ... but in the meantime this works nicely.

@OlivierLemoine
Copy link

OlivierLemoine commented Apr 27, 2023

I had a problem with a UTF-8 char.
That's why I propose to update the <head> with <meta charset="UTF-8"> inside.

@hinell
Copy link

hinell commented May 6, 2023

Anyone knows what's the backend is used to implement conversion from SVG to PS in chromium? Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment