Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create several pages with dompdf

I am having some trouble with multidimensional array and its value.

What i am looking for is , from my query I am searching teachers name in the array. And after that i want to create a pdf using dompdf. The problem is with looping. I am not able to create a proper loop which will work the way I want it to work. My sample query is

    $q11 = "select id from teachers order by teacher ";
    $r11 = mysql_query($q11) or die(mysql_error());
    while($rows11 = mysql_fetch_array($r11)){
        $teacher = $rows11['id'];
        $dompdf->"It will start working";
    }

Now i know , this code is confusing, but what i want is, it should create dompdf for every teacher in one single pdf file. Like from the query it should fetch teachers, and for each teacher it should create a dompdf page. Currently it is making just one page according to the last value that my query has search.

Please help. It is kinda urgent.

like image 941
I Perfect Avatar asked Dec 30 '12 10:12

I Perfect


People also ask

How do you do a page break in Dompdf?

How do you do a page-break in Dompdf? Using page-break-inside: auto; basically says to dompdf "do what you would normally do when breaking pages." To force a page break before/after your table you would use page-break-before: always; / page-break-after: always; .

How do I use Dompdf?

Basic Usage (Convert HTML to PDF) The following example shows how to use Dompdf to convert HTML and generate PDF with minimal configuration. Specify the HTML content in loadHtml() method of Dompdf class. Render HTML as PDF using render() method. Output the generated PDF to Browser using stream() method.

How do I save Dompdf generated content?

'<p>Put your html here, or generate it with your favourite '. 'templating system. </p>'. '</body></html>'; $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $output = $dompdf->output(); file_put_contents('Brochure.

How do I add a header and footer in Dompdf?

Just like the header, we will put our footer in a footer tag inside the body tag of the template. Just like header, we will create a space for our footer on every page then use part of the space for our footer. For the body content, put everything in a main tag inside the body tag of the template.


2 Answers

Your loop is working fine. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.

EDIT

I've never used dompdf. A quick look into the docs let me think you create something like a HTML markup which then is converted into PDF, did I get this right?

Example code

$html = <<<HTML
  <html>
      <head>
            <style type="text/css">
                /* Your document styling goes here */
            </style>
      </head>
      <body>
HTML;

while ( $row = $dbResult->fetch_assoc() ) {
    $html .= '<div class="teacherPage">'
                 . $row['name'] // your teacher document goes here
             '</div>';
}

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

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");

If you wonder about the unusual syntax $var = <<<HTML \r\nHTML, that's a heredoc. It's just more comfortable to use heredocs when you have a lot of alien inline code, this can have variables {$varname} and you don't need to worry about quotes. All you need to make sure, is that heredoc closer HTML is in a new line and not indented.

EDIT2

Still not too sure, which library you are using. I find this extension looking pretty good and it's called dompdf, just like you said in your question.

Your latest comment indicates you did not solve your problem so far, so I decided to add some more information to get you to the target.

Disclaimer: I am not going to write functional code and I will not test this, but the following hints will push you into the right direction to get your stuff done.

dompdf is able to read CSS2 and CSS3 properties of your input document.

Each cycle in the while loop above represents one teacher whith each of them getting a own page in the output document.

I put the page into a div container with the class teacherPage. You can fill this container with all the information you want to have displayed for a teacher.

Now all we need to do, is to tell dompdf each teacherPage is a new page. This can be done using @page markup shipped with CSS3

I added an empty css container <style type="text/css"></style> to the example document above, that's where the page styling should go to.

The example CSS

@page teacher {
  size: A4 portrait;
  margin: 2cm;
}

.teacherPage {
   page: teacher;
   page-break-after: always;
}

With @page you can define a named page teacher, which can have properties valid for the whole page container.

page-break-after: always will begin a new page after each container

Hope this helps, have fun trying :)

like image 95
Michel Feldheim Avatar answered Sep 30 '22 17:09

Michel Feldheim


Apparently, no overflow problems and the results are taking more than the permitted margin of the sheet. What you can do is to separate data from the parent in another table and put between them:

<div style="page-break-before: always;"></div>
like image 30
Walter Caraza Avatar answered Sep 30 '22 18:09

Walter Caraza