7

当我尝试在由 wicked_pdf (wkhtmltopdf) 生成的 PDF 中包含 SVG 时,它显示为空白。知道如何让 svg 显示在 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}

条码控制器

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

3 回答 3

8

我使用这种嵌入 SVG 的方法让它工作。

使用标签显示内联 SVG

代替

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

"data:image/svg+xml;base64,#{Base64.encode64(barcode.to_svg)}"
于 2012-10-11T12:52:04.730 回答
3

这就是我调整 user1632065 答案以在 html 和 pdf 中工作的方式

在你的 GemFile

gem 'cairo'
gem 'barby'

在你的模型中

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

在你看来(在我的情况下是haml)

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

这样你就可以得到正确的条形码宽度

于 2013-05-30T10:20:34.797 回答
1

我在我的一个 Rails 3.0.x 应用程序中使用了 wicked_pdf,它就像一个魅力。我使用直接嵌入到 HTML 中的 SVG 代码(erb/haml 视图)。否或 - 只是具有给定“宽度”和“高度”属性的纯标签。它不适用于某些浏览器(Opera,IE<9),但就我而言,我不在乎。也许你可以尝试这样处理你的东西?

于 2011-12-02T22:18:00.047 回答