how to add multiple pages in a pdf file using dompdf + php

9,873 views
Skip to first unread message

Shail paras

unread,
Apr 25, 2013, 6:50:40 AM4/25/13
to dom...@googlegroups.com
hi.

I am using dompdf to convert html to pdf. and my query is that:

Is it possible to add new page in same pdf file dynamically?

I have used :

page-break-after:always

it is not working.

my code is:

ob_start();   
    $html = "<style>@page { margin:0; } div.canvasbox {page-break-after:always;}</style>";
     foreach($allimagesData as $allimagesData)
       {
            $pagecontent = $allimagesData['page_content'];
            $html .= preg_replace('/<!--(.*)-->/Uis', '', $pagecontent);  // used to remove some commented lines from html string
        }                       
         echo $html;
 $contents = ob_get_clean();       
   
     $dompdf = new DOMPDF();
     $dompdf->load_html($contents);
     $dompdf->set_paper(array(0,0,12*72,12*72), 'portrait');
     $dompdf->render();              
     $pdf = $dompdf->output();                           
     file_put_contents(BASE_PATH."/public/testingpdf/sample.pdf", $pdf);


BASE_PATH is my root path.
$allimagesData is array which is contains pages html content.
and some regex used to get a rid of some commented lines from html string.

Currently it show me only first page... and i can not see my another pages..
Actually there are four pages ..
But It shows only first one.
What am i doing wrong? or missing something?

Thanks in Advance.

$hAil-pARAs

unread,
Apr 25, 2013, 10:00:13 AM4/25/13
to dom...@googlegroups.com
OR is there any way to merge pdf files?

BrianS

unread,
Apr 25, 2013, 11:42:16 AM4/25/13
to dom...@googlegroups.com
On Thursday, April 25, 2013 10:00:13 AM UTC-4, $hAil-pARAs wrote:
OR is there any way to merge pdf files?

Not with dompdf. You'd have to use another library or an app such as pdftk.

BrianS

unread,
Apr 25, 2013, 11:41:52 AM4/25/13
to dom...@googlegroups.com
If your content is correctly formatted paging should happen automatically. Using page-break-after: always; also works. There may be something blocking the content from paging, either due to some structural problems, a style applied, or even a bug. But we can't tell without seeing the actual HTML content.

You should first check the value of your $html variable to make sure it contains what you expect. For example, you're using a greedy regex selector (.*) instead of the less greedy (.*?) and maybe you're losing content.
Reply all
Reply to author
Forward
0 new messages