2

The image is of fixed size and I want to update that image for every user like updating the username and date etc. Then I want to convert that image to PDF with the A4 printing size. How can this be achieved in PHP/Laravel.

Abdul Jamil
  • 363
  • 3
  • 13

5 Answers5

1

I was able to achieve this by first creating a responsive html and then converting that html to PDF.

Abdul Jamil
  • 363
  • 3
  • 13
0

There are various libraries available for pdf generation for PHP.

e.g. there is MPDF which allows you to create a html and pass it to the library and it converts it into pdf.

https://github.com/mpdf/mpdf

You can also check a comparison of various other generators here: Which one is the best PDF-API for PHP?

Community
  • 1
  • 1
Nabeel Khan
  • 3,715
  • 2
  • 24
  • 37
  • Thanks Nabeel but I know about the libraries. The main challenge here is to edit the image and I think there are libraries available for that too. The other thing I am thinking of creating a responsive html and then convert to PDF. – Abdul Jamil Apr 27 '16 at 05:49
  • to add text to image (without removing anything) you can use that image as a background and add the text as html on it – Nabeel Khan Apr 27 '16 at 05:52
  • if this ans helped, please vote up and select as best :) – Nabeel Khan Apr 27 '16 at 09:14
0

You can look into the php_GD library and in particular, the imagettftext. For image to PDF conversions, try using ImageMagick

0

You can add text to an image with Intervention using text() method:

$img = Image::make('public/foo.jpg');
$img->text('The quick brown fox jumps over the lazy dog.', 120, 100);

Then, you could just create simple HTML wrap for an image to convert it to PDF with any appropriate tool or package:

$pdf = PDF::loadView('view.with.image', $data);
return $pdf->download('image.pdf');

Or, you could convert image to PDF using ImageMagick (CLI example):

convert image.jpg image.pdf
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

I would definitely take a look at using Snappy by barryvdh

One you have the package pulled in through composer. It's as simple as using the wrapper the package provides.

    $snappy = \App::make('snappy.pdf');

     $snappy->generateFromHtml("Link to your image here", '/tmp/NameofYourPDF.pdf'); 

This example is here is then saving it to the /tmp folder on the server.

Have used this in the past and it makes it very straight forward to use.

You need to have wkhtmltopdf/wkhtmltoimage installed. You can download wkhtmltopdf from http://wkhtmltopdf.org/downloads.html

andrew-caulfield
  • 2,060
  • 2
  • 23
  • 24