irslan's avatar

How to add an image watermark or image background in PDF file in Laravel

How to add an 'image watermark 'or image background in a PDF file in Laravel? Using this function: $(document).ready(function($) { $(document).on('click', '#generatePDF', function(event) { let invoice_number = $("#invoice_number").text() event.preventDefault(); var element = document.getElementById('print-invoice'); var opt = { margin: [0, 0, 30, 0], filename: 'Invoice_'+ invoice_number +'.pdf', image: { type: 'jpeg', quality: 0.98 }, backgroundimage:{url('images/lh.jpg')}, //THIS ONE IS NOT WORKING. html2canvas: { dpi: 192, scale: 5, letterRendering: true}, jsPDF: { unit: 'pt', format: 'a4', orientation: 'portrait'}, pageBreak: { mode: 'css', after:'.break-page'} }; html2pdf().set(opt).from(element).save(); }); });

Please help. I am not good at all in Laravel.

Thanks

0 likes
7 replies
Sinnbeck's avatar

That's Javascript? Nothing to do with laravel. There are several laravel pdf packages if you want to do it there.

But I guess it takes html + css and generate the pdf. So why don't you try setting a background image on the body

1 like
irslan's avatar

@Sinnbeck Thank you for your reply I tried this:

		background-image:url('/images/lh.jpg'); 
        width: 100%;
        height: 100%;
        background-size: cover;
        background-repeat: no-repeat;
        background-position: top;

But here I see one issue. If the output in the table is one row then 70% of the background image is shown and if there are 7 rows then I can see the whole background image coming on a full page. So please guide accordingly. Thanks

Sinnbeck's avatar

@irslan I'm not exactly sure what that means. Does the table cover the image? Make sure that the table and all cells have transparent background

irslan's avatar

@Sinnbeck Yes the table is covering the image but doesn't come on full page. The table is also transparent BUT as the table gets more data to show then the image starts to increase horizontally. If there are 5 rows in the table then i can see the full image background behind the table.

irslan's avatar

Thank you so much for showing me the right direction. Finally, I got able to fix my image background.

Many thanks

Sinnbeck's avatar

@irslan Great to hear. Please remember to set the thread as solved by marking a best answer

Please or to participate in this conversation.