Petros Mastrantonas
Petros Mastrantonas

Reputation: 1046

dompdf - scale the pdf to fit page

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));  

Upvotes: 14

Views: 27258

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 99031

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.

Upvotes: 1

Halayem Anis
Halayem Anis

Reputation: 7805

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

Upvotes: 3

Related Questions