WickedPDF本地渲染与生产不同

use*_*891 6 ruby fonts ruby-on-rails wkhtmltopdf wicked-pdf

我已经在我的台式机上安装了wkhtmltopdf 0.12.3(带有修补的qt)和我的Centos虚拟机上完全相同的版本.

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

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

图像抓取来自Centos VM上的PDF

你可以看到en之间的差距但是我的本地机器上没有.图片如下:

在此输入图像描述

有谁知道为什么会这样?

任何意见,将不胜感激.

谢谢

这是我使用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
Run Code Online (Sandbox Code Playgroud)

这里也是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
Run Code Online (Sandbox Code Playgroud)

我已经解决了这个问题:

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

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

Jas*_*der 2

我在某些特定字母之后遇到了类似的空格问题,例如

Reg istration  ---> space after letter 'g'

O pen   ---> space after Capital letter 'O'
Run Code Online (Sandbox Code Playgroud)

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

此问题出现在使用 CentOS 6.x 的生产实例 (AWS AMI linux) 上,但在 ubuntu 14.04 上运行良好。我在路径 /etc/fonts/conf.d 上查看了 CentOS 上的系统字体配置 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>
Run Code Online (Sandbox Code Playgroud)

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