9

I am using PHP, Mysql, jQuery. I have a webpage that is to be converted into high-res A4 size PDF: http://optisolbusiness.com/funeral_site/sample/index/id/255.

I have converted the HTML to PDF using wkhtmltopdf, which works great.

Here is the generated PDF http://optisolbusiness.com/Guru/Gurupdf/optisol.pdf. But the HTML is not fitting exactly to PDF size; There are spaces around the HTML in PDF. How to scale HTML to fit A4 PDF size 100%? Importantly the content inside the html (ie) text size, images width and height, background images also to be scaled proportionally.

halfer
  • 19,824
  • 17
  • 99
  • 186
PHPDev
  • 139
  • 1
  • 3
  • 12
  • This question should have stated that it was a follow-on from [this question](http://stackoverflow.com/q/12834641/472495), which I requested. This would have provided useful background information for respondents. – halfer Oct 12 '12 at 13:10
  • 1
    pages not foune :( – shareef Oct 30 '17 at 16:14

3 Answers3

10

Your background image is not exactly high-res, this won't look great in print.

I don't know wkhtmltopdf itself, but your body already has absolute dimensions set (in inches). This is probably the problem. Your body has a max size, the content has an absolute size too (given due to the background image pixel dimensions).

This is not a good starting point for html-to-print transformaions, and PDF is essentially print.

what to do (intermediate)

  • remove any size restrictions from body
  • wkhtml... has a switch called zoom, 1.5 should be an appropriate value to fill the page
  • use page-size a4

what to do (the "right" way)

  • remove size restrictions from body
  • build the background borders (the black ones) with html elements and css styling
    • refrain from defining "width" rules for those. You will only have to define a "width" once, all other widths should be set to "auto".
    • heights will prove troublesome, because divs are only as high as their content requires. But setting height: 100% does not respect border and margin sizes.
  • that yellow cross could be designed in css too, or a much higher resolution png/jpeg
  • Use only "real" dimensions. That means do not use pixels, use points, inches, or mm. You can use % values, but make sure those are % values of real dimensions (that means that at some point a parent element has a real dimension)
dualed
  • 10,262
  • 1
  • 26
  • 29
  • 3
    Thanks a lot wkhtmltopdf zoom option is working great.you saved me lot of time.Works awesome,great,perfect,dualed you are great.Many thanks – PHPDev Oct 12 '12 at 11:27
  • @Guru: If it's anything that will eventually become automated and permanent, you should really follow my 2nd part. With a bit care it will not be very hard to achieve and the output quality will be higher. Keep in mind that pixel dimensions are subject to DPI scaling and thus your output may vary over time, platforms, and programs. Also the required value for the scale parameter depends on the size of the content, which is not good for maintainability. Also the HTML view would more closely resemble the PDF output. – dualed Oct 12 '12 at 13:16
  • I am using Wkhtmltopdf, windows OS,Xampp to convert html into A4 PDF documents from windows command.it works fine.But how can i achieve this by PHP. Windows command i use is D:\Program Files\wkhtmltopdf>wkhtmltopdf.exe http://www.google.com E:/mypdfs/d.pdf works fine from command line.How can i do this with PHP Can anyone help me.Any suggesstions – PHPDev Oct 12 '12 at 13:28
  • when looking at the wkhtmltopdf site I found a link to [this project implementing PHP bindings](https://github.com/mreiferson/php-wkhtmltox) may want to look at it. Otherwise there is a function in PHP called [exec](http://php.net/exec) which lets you run any command. Either way, you will have to install wkhtmltopdf on your XAMPP server; There should be a package available for your distribution if it's linux. – dualed Oct 12 '12 at 13:41
  • I am getting some trouble with page height, I defined it to 164mm in command line and i create multiple pages in my document using a div block with a .page class. For 1 or 2 pages, everything is ok but from 3 pages, I am getting an empty fourth page. After some tests, I saw that the last page is overflowing with 1px out (no px unit are used in css, i minimized the use of unit to fit automatically the PDF page). – Loenix Jan 12 '17 at 09:55
1

I'd say that you're always going to struggle to get this to be perfect. In my opinion you're better off writing the PDF directly rather than relying on a third party tool.

Consider looking into FPDF, an open-source PHP PDF writing library. Be warned, the website looks out of date, but the functionality works beautifully.

Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
  • Anything where you're asking for "exactly" and generating PDFs, you should definitely look at a library like FPDF. I've used it in the past. It takes a little getting used to and the resulting code is as ugly as all hell but you can essentially produce whatever you want, pixel for pixel. – Joshua Pinter Jun 28 '15 at 15:02
0

You can set the size of the body to the size of the page.. in case of A4 that is 210x297mm.

Btw: you should only be using width in percentage, otherwise wkhtmltopdf will have to try and convert it.

So make sure you use width: 100%; if you want it to fill all the room. ;)

BTW: If you want real high quality PDFs you will need to create them conforming to at least the PDF/X-3 standard. I don't think wkhtmltopdf does that tho.

Gung Foo
  • 13,392
  • 5
  • 31
  • 39