wkhtmltopdf keep font size issue

情到浓时终转凉″ 提交于 2020-01-24 20:55:06

问题


I'm converting a html file to pdf with wkhtmltopdf 0.12.1.2 and the font sizes are different on the output, the font size is 13.68 but it's 11pt in the html. Any help on getting it to match?

  <span style="font-family: Calibri; font-size: 11pt; ">
  <table>
    <tbody>
     <tr><td>this is a test Calibri 11pt font</td></tr>
etc etc


         wkhtmltopdf32.exe --disable-smart-shrinking --page-width 215.900000 --page-height 279.400000 1.htm 1.pdf

even if I remove the page size it's still the same.


回答1:


This issue is caused during the html parsing by QT. It depends also on the font used. If we extract the font style and check the size of font, we can get some idea about the conversion of size from point to pixels. When I checked the conversion of point to pixel in Explorer/Google Chrome the pixel value is not as shown below. Below values are retrieved by running a JavaScript from wkhtmltopdf in the time of PDF conversion.

In case of Arial the below given is the conversion.

8pt   -> 10px
9pt   -> 12px
10pt  -> 13px
11pt  -> 14px
12pt  -> 16px
14pt  -> 18px
16pt  -> 21px
18pt  -> 24px
20pt  -> 26px
22pt  -> 29px
24pt  -> 32px
26pt  -> 34px
28pt  -> 37px
36pt  -> 48px
48pt  -> 64px
72pt  -> 96px

Then, to convert them to the required size, I wrote another method to replace the above list of pixel values by the pixel values that gives the expected output in PDF. The conversion for Arial font was like shown below :

10px -> 11.5px
12px -> 13.5px
13px -> 14.5px
14px -> 15.95px
16px -> 17px
18px -> 20px
21px -> 23.5px
24px -> 26.25px
26px -> 28.5px
29px -> 31.5px
32px -> 34.5px
34px -> 37.5px
37px -> 40.5px
48px -> 51.75px
64px -> 69px
96px -> 104px

This solved my issue and gave the exact matching font in PDF.



来源:https://stackoverflow.com/questions/39337284/wkhtmltopdf-keep-font-size-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!