WickedPDF Rendering differently locally vs production
Asked Answered
M

1

7

I have installed wkhtmltopdf 0.12.3 (with patched qt) on my desktop machine and the exact same version on my Centos virtual machine.

I am using WickedPDF and Rails to convert HTML to PDF.

When the PDF is generated it has slight differences in the font. Some weird letter spacing. I have attached images showing this.

Image grab from PDF on Centos VM

You can see the gap between the e and n this however is not present on my local machine. Image below:

enter image description here

Does anyone know why this would happen?

Any advice would be appreciated.

Thanks

This is the Ruby code which I am using to generate the PDF with WickedPDF

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

Here is also the show.pdf.haml file with the CSS at the top:

!!! 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

I have since fixed this issue by following this:

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

It fixes kerning issue on CentOS for me.

Missilery answered 15/2, 2016 at 17:5 Comment(9)
As per the how to ask a question on Stackoverflow, please show the relevant code.Hautboy
I have updated with the code that is being used.Missilery
fairly obvious followup: you're using TradeGothic as your font. Do both systems have the exact same version of that font available?Hautboy
I have installed the font on both systems, using the same source font file.Missilery
your VM is CentOS, which OS is the host?Hautboy
My local machine is Linux Mint. Do you think that would make a big difference?Missilery
All of this is important information to put in the post. Especially if the OS are difference, the text shaper might be different, which would invalidate any and all assumptions around "the same fonts will look the same".Hautboy
Gotcha. Well. Thanks for helping me investigate. I will not try and install the same OS as a server. Thanks again.Missilery
The link in the question requires login.Please link to something publicly accessible.Gazzo
F
2

I was facing a similar issue of spaces after certain specific letters like

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

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

The above issue could be due to system specific fonts configs. I referred an issue reported over github for the same https://github.com/wkhtmltopdf/wkhtmltopdf/issues/45

This issue occurs on a production instance (AWS AMI linux) with CentOS 6.x but was working perfectly with ubuntu 14.04. I looked into the system fonts configs 51-local.conf over CentOS at path /etc/fonts/conf.d .If there is no file with name wkhtmltopdf at the same path then use the default fonts file 51-local.conf to have the below configuration as custom fonts.

<?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>

Also, If you are using wicked_pdf.rb initializer configuration exec path as /usr/local/bin/wkhtmltopdf for development environment. try to replace the path with /bin/wkhtmltopdf for production bundle

Franklynfrankness answered 28/9, 2018 at 11:44 Comment(1)
I had this exact same issue (worked on Ubuntu 14.04, random spacing between characters on AWS AMI Linux), this fix did the trick. Thank you!Mallis

© 2022 - 2024 — McMap. All rights reserved.