Embedding SVG in PDF using Wicked_PDF (wkhtmltopdf)

When I try to include SVG in a PDF file created by wicked_pdf (wkhtmltopdf), it looks empty. Any idea how to get SVG to display in pdf?

app / views / barcodes / to_pdf.html.haml

<descriptive text here> %object#code_image{:data=>"/barcodes/generate_svg?code=4567898", :type=>"image/svg+xml", :height=>70} 

barcode controller

 def generate_svg require 'barby' require 'barby/barcode/code_128' require 'barby/outputter/svg_outputter' barcode = Barby::Code128B.new(params[:code]) render :text => barcode.to_svg({:height=>30, :width => 170}) end def to_pdf render :pdf => 'file_name' end 
+4
source share
3 answers

I got it to work using this SVG embed method.

Show Inline SVG with tag

Replace

 "data:image/svg+xml;base64,PD94bWwgdmVy..." 

with

 "data:image/svg+xml;base64,#{Base64.encode64(barcode.to_svg)}" 
+6
source

this is how i adapted user1632065 to work in html and pdf

in your gemfile

 gem 'cairo' gem 'barby' 

in your model

 class ExampleModel def barcode require 'barby' require 'barby/barcode/code_128' require 'barby/outputter/cairo_outputter' Barby::CairoOutputter.new(Barby::Code128B.new('bla bla this is barcode source')) end end 

in your opinion (in my case haml)

 %img{:width => ExampleModelObject.barcode.width, :src=> "data:image/svg+xml;base64,#{Base64.encode64(ExampleModelObject.barcode.to_svg)}"} 

this way you get the correct barcode width

+3
source

I use wicked_pdf in one of my Rails 3.0.x applications and it works like a charm. I use SVG code embedded directly in HTML (erb / haml views). No or - only a clean tag with the specified attributes "width" and "height". This does not work for some browsers (Opera, IE <9), but in my case it does not bother me. Maybe you could try to go with your things this way?

+1
source

All Articles