1

I would like to generate PDFs from HTML (twig), including the page numbers, and followed this example:

public function returnPDFResponseFromHTML($html){
    //set_time_limit(30); uncomment this line according to your needs
    // If you are not in a controller, retrieve of some way the service container and then retrieve it
    //$pdf = $this->container->get("white_october.tcpdf")->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    //if you are in a controlller use :
    $pdf = $this->get("white_october.tcpdf")->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetAuthor('Our Code World');
    $pdf->SetTitle(('Our Code World Title'));
    $pdf->SetSubject('Our Code World Subject');
    $pdf->setFontSubsetting(true);
    $pdf->SetFont('helvetica', '', 11, '', true);
    //$pdf->SetMargins(20,20,40, true);
    $pdf->AddPage();

    $filename = 'ourcodeworld_pdf_demo';

    $pdf->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = '', $autopadding = true);
    $pdf->Output($filename.".pdf",'I'); // This will output the PDF as a response directly
}

It basically works, but I'm trying to add page numbers, which is not working:

    // ...
    $pdf->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = '', $autopadding = true);

    $y = 10; // later replaced by the real position
    for ($i = 0, $c = $pdf->getNumPages(); $i < $c; $i++) {
        $pdf->writeHTMLCell(0, 0, 0, $y, "PAGE $i");
        $y += 10;
    }

    $pdf->Output($filename.".pdf",'I'); // This will output the PDF as a response directly

Problem: the page numbers are added to the last page. I tried $y = -10, but this doesn't add anything to the previous page; it just hides the page number.

Does anyone know, how I could add page numbers using writeHTMLCell()?

Thanks in advance!

Mr. B.
  • 8,041
  • 14
  • 67
  • 117
  • Have you considered KnpSnappyBundle instead? Documentation is good... Also, in the above you show `"PAGE $i"`, but shouldn't that be: `"PAGE" . $i` instead (concatenate)? – Alvin Bunk Sep 06 '16 at 16:12
  • @AlvinBunk Thanks for your answer. I'll have a look at `KnpSnappyBundle`. As far as I know, `"PAGE $i"` (double-quote) is correct; for single-quote the syntax is `'PAGE ' . $i`. Double-quotes allow you to avoid concatenating in this case. More about **double quotes vs. single quotes**: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Mr. B. Sep 06 '16 at 17:25

0 Answers0