Dompdf Page Size Assistance

7,622 views
Skip to first unread message

carter

unread,
Aug 6, 2016, 12:16:15 AM8/6/16
to dompdf
Hello, I am looking for assistance on setting a custom page size in dompdf version 0.6.0. This is likely an easy solution; I am not an advanced programmer. Here is the issue:

In my dompdf.php file, I see the following commands (interspersed):

function dompdf_usage() {
  $default_paper_size
= DOMPDF_DEFAULT_PAPER_SIZE;
 
  echo
<<<EOD
 
Usage: {$_SERVER["argv"][0]} [options] html_file

html_file can be a filename
, a url if fopen_wrappers are enabled, or the '-' character to read from standard input.

Options:
 
-h             Show this message
 
-l             List available paper sizes
 
-p size        Paper size; something like 'letter', 'A4', 'legal', etc.  
                 
The default is '$default_paper_size'
 
-o orientation Either 'portrait' or 'landscape'.  Default is 'portrait'
 
-b path        Set the 'document root' of the html_file.  
                 
Relative urls (for stylesheets) are resolved using this directory.  
                 
Default is the directory of html_file.
 
-f file        The output filename.  Default is the input [html_file].pdf
 
-v             Verbose: display html parsing warnings and file not found errors.
 
-d             Very verbose: display oodles of debugging output: every frame
                 
in the tree printed to stdout.
 
-t             Comma separated list of debugging types (page-break,reflow,split)
 
EOD
;
exit;
}

$sapi
= php_sapi_name();
$options
= array();

switch ( $sapi ) {

 
case "cli":

  $opts
= getoptions();

 
if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
    dompdf_usage
();
   
exit;
 
}
 
- -

 
if ( isset($opts["l"]) ) {
    echo
"\nUnderstood paper sizes:\n";

   
foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
      echo
"  " . mb_strtoupper($size) . "\n";
   
exit;
 
}
  $file
= $opts["filename"];

 
if ( isset($opts["p"]) )
    $paper
= $opts["p"];
 
else
    $paper
= DOMPDF_DEFAULT_PAPER_SIZE;

 
if ( isset($opts["o"]) )
    $orientation
= $opts["o"];
 
else
    $orientation
= "portrait";

- -

$dompdf
= new DOMPDF();

if ( $file === "-" ) {
  $str
= "";
 
while ( !feof(STDIN) )
    $str
.= fread(STDIN, 4096);

  $dompdf
->load_html($str);

} else
  $dompdf
->load_html_file($file);

if ( isset($base_path) ) {
  $dompdf
->set_base_path($base_path);
}

$dompdf
->set_paper($paper, $orientation);

$dompdf
->render();

if ( $_dompdf_show_warnings ) {
 
global $_dompdf_warnings;
 
foreach ($_dompdf_warnings as $msg)
    echo $msg
. "\n";
  echo $dompdf
->get_canvas()->get_cpdf()->messages;
  flush
();
}




In my project file:

  
$dompdf = new DOMPDF();
       
        $dompdf
->load_html_file("dompdf/output/" . $outputFile);
        $dompdf
->render();
        $outputPDF
= $dompdf->output();
        file_put_contents
($_REQUEST['folder'] . '/' . $firstRecipe . '.pdf', $outputPDF);

   
} else {

       
//clear all the output, nothing will be displayed.
       
if($pdf) ob_end_clean();

       
//Generate unique file name, time() gives
       
//current time in milliseconds so it is always different
        $outputFile
= time() . ".html";
        file_put_contents
("dompdf/output/" . $outputFile, $output);

       
//now lets redirect to the PDF converter!
       
//You can change some settings for the PDF viewer in the URL:
        $url
= "dompdf/dompdf.php?base_path=output%2F&options[Attachment]=0&input_file=" . $outputFile . "#toolbar=1&view=FitW&statusbar=1&messages=0&navpanes=1";
       
if($pdf) header("Location: $url");

   
}

What command do I need to include in my project file to change the size to a landscape 4" x 6" paper size (not A6)?

I appreciate any assistance, thank you!

BrianS

unread,
Aug 6, 2016, 1:37:51 AM8/6/16
to dompdf
Before I talk about the page size issue I just need to point out that there are some fairly serious security bugs in 0.6.0. I'd consider updating to dompdf 0.6.2 which addresses these. It's a drop-in replacement for 0.6.0 so upgrading should not require too much effort.

Paper sizes for PDFs are defined in points defaulting to 72 PPT (pt per inch). So the dimensions of your page would be 288pt x 432pt. Since this is not a size defined in the standard page sizes you're have to define it manually to dompdf. This is done by passing an array of the page width and height, and two zeros (e.g. array(288, 432, 0, 0)).

Now to actually set the paper size depends on how you use dompdf. If you always plan to use this size you can set it in your dompdf_config.custom.inc.php configuration file. Look for the DOMPDF_DEFAULT_PAPER_SIZE configuration constant.

If you don't want to mess with that file you can also set it at run time.

You shouldn't need to use the dompdf.php file at all. You have everything you need to render a PDF without it. Unless your post is missing an important bit of code (which is may very well be since this appears to be part of an if/else statement). You would be able to fully generate a file using just the class like this:

$dompdf->load_html_file("dompdf/output/" . $outputFile);
// *OR* if you have HTML $dompdf->load_html($output);
$dompdf
->set_paper(array(288,432,0,0));
 $dompdf
->render();
 $dompdf
->stream('document.pdf');



I recommend removing the file completely, but if you do want to still use dompdf.php I think the only way to set the paper size is to use the configuration constant.

carter

unread,
Aug 9, 2016, 11:20:53 AM8/9/16
to dompdf
Thank you for your assistance, this helps! I am still running into issues, but I am getting more specific error messages.

I have started a new file from scratch to remove all the variables. Here is what I have:

<?php require_once("dompdf/dompdf_config.inc.php"); ?>

<?php

$dompdf
= new DOMPDF();

$html
=
 
'<html><body>'.
 
'<p>Sample html.</p>'.
 
'</body></html>';
 

$dompdf
->load_html($html);
//$dompdf->set_paper(array(288,432,0,0));
$dompdf
->render();
$dompdf
->stream("sample.pdf");

?>



This file runs successfully as expected. When I remove the comment on the set_paper function, I get the following error:

Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'Box property calculation requires containing block width' in [my internal filepath]/dompdf/include/block_frame_reflower.cls.php:171.


I have reviewed this block_frame_reflower.cls.php php file, and it looks like the $w variable isn't getting set. I am not sure how that is supposed to work. What am I missing?

Thank you for your assistance!

BrianS

unread,
Aug 10, 2016, 2:28:15 PM8/10/16
to dompdf
Is that HTML sample what you've actually tried? I'm not seeing the same issue.

The exception is not unknown though we have yet to address it.
Reply all
Reply to author
Forward
0 new messages