WickedPDF本地渲染与生产不同

时间:2016-02-15 17:05:42

标签: ruby-on-rails ruby fonts wkhtmltopdf wicked-pdf

我在桌面计算机上安装了 wkhtmltopdf 0.12.3(带有修补的qt),并且在我的Centos虚拟机上安装了完全相同的版本。

我正在使用WickedPDF和Rails将HTML转换为PDF。

生成PDF时,字体略有不同。一些奇怪的字母间距。我附上的照片显示了这一点。

Image grab from PDF on Centos VM

您可以看到 e n 之间的差距,但本地计算机上不存在这种差距。图片如下:

enter image description here

有谁知道为什么会这样?

任何建议都将受到赞赏。

由于

这是我使用WickedPDF生成PDF的Ruby代码

def generate_pdf
    pdf = WickedPdf.new

    pdf_file = pdf.pdf_from_string(ActionController::Base.new().render_to_string('card_requests/show.pdf.haml', layout: 'pdf', locals: {card_request: self}),
      dpi: '300',
      margin: {top: 0, bottom: 0, left: 0, right: 0},
      page_height: '2.25in',
      page_width: '3.75in',
      disable_smart_shrinking: false
    )

    tempfile = Tempfile.new(["#{self.name.gsub(' ', '_').downcase}_biz_card", '.pdf'], Rails.root.join('pdfs'))
    tempfile.binmode
    tempfile.write pdf_file
    tempfile.close

    self.pdf = File.open(tempfile.path)
    tempfile.unlink

    self.save

  end

这里也是show.pdf.haml文件,顶部是CSS:

!!! 5
%html
  %head
    :css
      html * {
        margin: 0 !important;
        padding: 0 !important;
        page-break-inside: avoid !important;
      }
      body {
        margin: 0 !important;
        padding: 0 !important;
        page-break-inside: avoid !important;
        text-rendering: optimize-speed;
      }
      .card-preview {
        font-family: 'TradeGothic';

        background-size: contain;
        width: 369px;
        height: 220px;
        page-break-after: avoid !important;
        position: relative;
      }

      #card-name {
        color: #ED1D27;
        font-weight: bold;
        font-size: 12pt;
        position: absolute;
        top: 37px;
        left: 39px;
        width: 328px;
      }

      #card-title {
        color: #2E2D2D;
        font-weight: bold;
        position: absolute;
        top: 54px;
        left: 39px;
        font-size: 9pt;
      }

      #card-office-phone {
        color: #4e4e48;
        position: absolute;
        top: 148px;
        left: 39px;
        font-size: 8.5pt;
      }

      #card-cell-phone {
        color: #4e4e48;
        position: absolute;
        top: 135px;
        left: 39px;
        font-size: 8pt;
        width: 200px;
      }

      #card-email {
        color: #4e4e48;
        position: absolute;
        top: 161px;
        left: 39px;
        font-size: 8.5pt;
      }

      #card-website {
        color: #4e4e48;
        position: absolute;
        top: 174px;
        left: 39px;
        font-size: 8pt;
      }
      .hide {
        display: none;
      }
    %meta{:charset => "utf-8"}
  %body
    .card-preview{style: "background-image: url('#{Rails.root.join('app', 'assets', 'images', 'card_template_2.svg')}')"}
      #card-name
        = card_request.name
      #card-title
        = card_request.title
      #card-office-phone{class: (card_request.office? ? "" : 'hide')}
        == office 555 555 5555 #{card_request.ext? ? "ext #{card_request.ext_phone}" : ''}
      #card-cell-phone{class: ((card_request.cell? && !card_request.cell_phone.blank?) ? "" : 'hide'), style: (card_request.office? ? "" : 'top: 148px; left: 39px;')}
        = (card_request.cell? ? "cell 555 555 5555" : '')
      #card-email
        = card_request.email
      #card-website
        www.website.com

我已经解决了这个问题:

https://taskman.eionet.europa.eu/issues/20890

它为我修复了CentOS上的字距调整问题。

1 个答案:

答案 0 :(得分:2)

在某些特定字母(例如

)之后,我也遇到了类似的空格问题
Reg istration  ---> space after letter 'g'

O pen   ---> space after Capital letter 'O'

上述问题可能是由于系统特定的字体配置引起的。我提到了通过github报告的同一https://github.com/wkhtmltopdf/wkhtmltopdf/issues/45

问题

此问题在具有CentOS 6.x的生产实例(AWS AMI linux)上发生,但在ubuntu 14.04上运行良好。我在CentOS的/etc/fonts/conf.d目录下查看了系统字体configs 51-local.conf。如果在同一路径下没有名称为wkhtmltopdf的文件,请使用默认字体文件51-local.conf以下配置为自定义字体。

<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
 <match target="font">
  <edit mode="assign" name="rgba">
   <const>rgb</const>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="hinting">
   <bool>true</bool>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="hintstyle">
   <const>hintslight</const>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="antialias">
   <bool>true</bool>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="lcdfilter">
   <const>lcddefault</const>
  </edit>
 </match>
</fontconfig>

此外,如果您将wicked_pdf.rb初始化程序配置执行路径用作开发环境的/usr/local/bin/wkhtmltopdf。尝试用/bin/wkhtmltopdf替换生产捆绑包的路径