Jump to content

Invoice PDF background problem


ThePostman

Recommended Posts

I'm trying to use a background in my PDF invoices. However I do not get any output.

 

Based on the TCPDF documentation (http://www.tcpdf.org/examples/example_051.phps) I used this code in invoicepdf.tpl to insert a background:

<?php

# Logo background (A4 size)
$pdf->Image(ROOTDIR.'/images/invoicebackground.png', 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$pdf->setPageMark();

# Invoice Status
...

 

After about a minute the script ends without showing any output. After turning on extra PHP logging I got this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 7440 bytes) in /home/whmcs/public_html/includes/classes/TCPDF/tcpdf.php on line 7269

 

I think this implies a memoryleak, but I'm not sure how to proceed. The PNG I'm working with is less then 90Kb.

 

Has anyone got this working with a background?

Link to comment
Share on other sites

Have you tried using some other image file or JPEG format?

 

Also, there are two rules you need to think about when using image backgrounds in TCPDF:

 

1. always set your background image before you call any of the writing functions, like WriteHTML, Multicell and so on

2. always call the setPageMark() function after you set your background image

 

Since you're setting your background image inside the invoice template itself, have in mind that the Header() function executes first...do you use the Header() function? If so, set your bg image inside the Header() function.

 

If nothing else works, try upgrading your TCPDF files?

Link to comment
Share on other sites

I've done some extra debugging. The problem seems to house in the alpha channel processing of PNG backgrounds.

 

I've made two changes.

 

First of all I've made a PNG image without transparency (alpha). I still prefer the PNG option over JPG as the files only get bigger and PDF generation on the server increases with JPG. However JPG also works.

 

Secondly I've used some new invoicepdf.tpl code due to weird margins being printed in the invoice (which results in the background not filling the page:

-----------------

# Logo background

$bMargin = $pdf->getBreakMargin();

$auto_page_break = $pdf->getAutoPageBreak();

$pdf->SetAutoPageBreak(false, 0);

$pdf->Image(ROOTDIR.'/images/invoicebackground-noalpha.png', 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);

$pdf->SetAutoPageBreak($auto_page_break, $bMargin);

$pdf->setPageMark();

------------------

 

I would have to test if this works on multipage invoices, but for now the issue seems solved for my (single page) invoices.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated