14

So I am using dompdf to create pdfs of html. Which works really good, the only problem is when the html is too big and dompdf creates it over two pages. Is there a way maybe in dompdf or in html to scale it to one page only?

Help would be greatly appreciated! Thank you!

Here is my config and my code to create pdfs.

define("DOMPDF_ENABLE_HTML5PARSER", true);
define("DOMPDF_ENABLE_FONTSUBSETTING", true);
define("DOMPDF_UNICODE_ENABLED", true);
define("DOMPDF_DPI", 120);
define("DOMPDF_ENABLE_REMOTE", true);

require_once 'dompdf/dompdf_config.inc.php';

$dompdf = new \DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait");
$dompdf->render();
$dompdf->stream("page.pdf", array("Attachment" => 0));  
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Petros Mastrantonas
  • 806
  • 1
  • 15
  • 41
  • html too big? do you have a screenshot to show us what's up running on the current condition. – Eric T Apr 30 '15 at 01:24
  • the html consist of a large jpg at the beginning and variable text under the jpg. the text can be long at times, so it would be good if dompdf could scale everything into a nice pdf. you can almost do it using the dpi setting. so right now i have a preview with the pdf and a select box where you can choose the dpi until it fits into a page. it would be good if dom pdf would do that automatically with a setting – Petros Mastrantonas Apr 30 '15 at 06:51
  • If you can live with a multiple of a set size then I've previously outlined a method here: http://stackoverflow.com/q/26691304/264628. – BrianS May 01 '15 at 21:00
  • Using your method you could do something similar by looping through the rendering process with different dpi settings until you get a single page. – BrianS May 01 '15 at 21:03
  • sounds interesting do you have an example how i would accomplish something like that? – Petros Mastrantonas May 02 '15 at 17:54
  • If you could link us to a sample HTML page that is large enough to produce the undesirable effect, that would be really helpful. – merlin2011 May 03 '15 at 23:44

2 Answers2

3

Sorry, but there is NO setting in DOMPDF to do that.
To achieve your needs, you have to play with CSS files to custom your images size or text font size

Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
1

DOMPDF has some issues with css and tables. Best practices are to avoid cell or rowspans and not using external css files. Also only a limited set of css rules are supported. Check out the full CSSCompatibility list.

To fix your problem, have you try setting the inline style of the img to style="max-width:100%; max-height:100%;" ?

Here's a full example of scaling a large image (4,256px × 2,832px) inside one page :

<?php 
$html = <<< LOL
<html>
<head>
</head
<body>
<div width="100%"><img style="max-width:100%; max-height:100%;" src="http://www.norrbottenbigband.com/upload/images/norrbotten-big-band_hq.jpg"> </div>

</body>
</html>
LOL;

define("DOMPDF_ENABLE_HTML5PARSER", true);
define("DOMPDF_ENABLE_FONTSUBSETTING", true);
define("DOMPDF_UNICODE_ENABLED", true);
define("DOMPDF_DPI", 120);
define("DOMPDF_ENABLE_REMOTE", true);
//define("DOMPDF_ENABLE_JAVASCRIPT", true);
//define("DOMPDF_ENABLE_CSS_FLOAT", true);

require_once 'dompdf_config.inc.php';

$dompdf = new \DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("a4", "portrait");
$dompdf->render();
$dompdf->stream("page.pdf", array("Attachment" => 0)); 

If none of the above works, try putting the output pdf inside a div with a specific size.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268