Created
January 3, 2014 10:46
-
-
Save redfield/8236061 to your computer and use it in GitHub Desktop.
Converting SVG to PDF using HyPDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'hypdf' | |
require 'open-uri' | |
require 'base64' | |
# SVG file from wikipedia article | |
svg = open('http://upload.wikimedia.org/wikipedia/commons/1/12/%D0%9F%D1%80%D0%B8%D0%BC%D0%B5%D1%80_%D1%87%D0%B5%D1%80%D1%82%D0%B5%D0%B6%D0%B0_%D0%B2_SVG_%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%82%D0%B5.svg').read | |
# Embedding SVG to HTML via <img> tag | |
html = "<html><img src='data:image/svg+xml;base64,#{Base64.encode64(svg)}'></html>" | |
# Converting to PDF | |
hypdf = HyPDF.htmltopdf(html, test: true, orientation: 'Landscape') | |
# Writing PDF to file | |
File.open('file_name.pdf', 'wb').write(hypdf[:pdf]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment