skip to Main Content

I’ve a pdf file of a rule that need to pass a several approval. In the end of the file (content), I need to:

  1. Add a QR Code as an e-sign in the specific place, or
  2. Add a border-less table that include some text and a QR Code after the last content of the pdf. Which mean if the last content of pdf ends in the height(x) of 200, then the content should placed in (as an example) the height(x) of 210.

Example

Is that possible?

TCPDF writeHtml will place the generated html in the top of the page, while writeHTMLCell and MultiCell will need a specific x and y to be defined.

I’m also trying in MPDF, but it stuck like the TCPDF.

The code before writing the html looks like this,

$pageCount = PDF::setSourceFile($file);
$template = PDF::importPage($i);
$size = PDF::getTemplateSize($template);
PDF::AddPage($size['orientation'], array($size['width'], $size['height']));
PDF::useTemplate($template);

// After this, I need to call writeHtml, writeHTMLCell, or MultiCell, but none of those met what I need.
// Or placing an image with PDF::Image also need to define the x and y
...
// PDF::SetX()
// PDF::SetY()
// PDF::Image($qrPath, null, null, 12, 12, 'PNG');
// PDF::WriteHTML(view('document-requests.approver.stamp', ['qr' => $qrPath]), true, false, true, false, '');
PDF::Output($file, 'F');

#Edit

Another thing from mPDF is that there’s an Overwrite method, but it’s only overwrite the content created/generated by the library itself.

2

Answers


  1. Answer:

    It is possible to add a QR code to a specific location in a PDF using TCPDF or MPDF, but it will require some additional work, as none of the built-in functions directly meet your needs.

    1. TCPDF:

    To add a QR code to a specific location in a PDF using TCPDF, you can use this code :

    <?php
    
    require_once('tcpdf.php');
    
    // Create a new PDF document.
    $pdf = new TCPDF();
    
    // Add a new page to the PDF document.
    $pdf->AddPage();
    
    // Set the font for the QR code.
    $pdf->SetFont('helvetica', '', 10);
    
    // Generate the QR code.
    $qrCode = $pdf->write2DBarcode('http://www.example.com', 'QRCODE,H', 10, 10, 30, 30, '', '');
    
    // Position the QR code at the desired location on the page.
    $pdf->SetX(100);
    $pdf->SetY(100);
    
    // Output the PDF document.
    $pdf->Output('example.pdf');
    
    ?>
    
    1. MPDF:

    To add a QR code to a specific location in a PDF using MPDF, you can use Code :

    <?php
    
    require_once('mpdf/mpdf.php');
    
    // Create a new PDF document.
    $mpdf = new mPDF();
    
    // Set the drawing font for the QR code.
    $mpdf->SetDrawingFont('helvetica', '', 10);
    
    // Generate the QR code.
    $qrCode = $mpdf->Image($qrPath, null, null, 12, 12, 'PNG');
    
    // Position the QR code at the desired location on the page.
    $mpdf->SetXY(100, 100);
    
    // Output the PDF document.
    $mpdf->Output('example.pdf');
    
    ?>
    
    • If you need to add QR Code in your PDF then you need to add this code where you need to put.
    $pdf->WriteHTML('<table><tr><td><img src="qr_code.png" /></td><td>Some text</td></tr></table>');
    
    • To position the table at the end of the PDF, you can use the SetY() function to set the Y position to the height of the PDF document. You can get the height of the PDF document using the GetPageHeight() function. You can use the below code when you are using TCPDF to position the table:
    $pdf->SetY($pdf->GetPageHeight());
    

    Once you have positioned the table at the end of the PDF, you can output the PDF document.

    I hope this helps to you.

    Login or Signup to reply.
  2. Hi,
    I use TCPDF in Laravel directly imported via composer. Using the package, you can call

    $page_dimensions = $pdf->getPageDimensions($page_number)

    With the package you are using, it would be (I think):

    $page_dimensions = PDF::getPageDimensions($template);

    Returns an array of page dimensions ref here TCPDF docs.

    The dimensions you would need are from

    $this->pagedim[$this->page]['MediaBox']

    So something like:
    $page_dimensions['MediaBox']['lly'] = lower-left y coordinate in points of the ‘meaningful page content’.

    I also use SetaSign Free PDF Document Importer (I use a few of their excellent products), which can extend TCPDF – their example shows importing a file via its MediaBox again its meaningful content.

    Anyhow, I hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search