Search code examples
phplaravelpdftcpdfmpdf

How to place a table or image dynamically to pdf in PHP


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.


Solution

  • Stuck for looking an automated way, I use a Client Side approach, and written the full way here. But for a glance, the steps are:

    1. User load PDF using pdf.js (from Mozilla), place it in a canvas,
    2. Use interact.js for making a div draggable, and assume that draggable-div as a QR to be placed,
    3. Could some calculation, since html canvas return a px while TCPDF return in cm.

    The results so far have been good