kfirba's avatar
Level 50

Convert image files and docs to PDF

Hello!

I was wondering how can I convert image files(JPEG,png, etc.) and docs (.doc, .docx, .xls, .txt, etc.) to a PDF file?

0 likes
9 replies
jlrdw's avatar

Microsoft Office 2007 Word does this.

kfirba's avatar
Level 50

@jlrdw I guess I wasn't clear. I need to do it in my application. The user uploads a file from the list specified above, and I need to conver it.

kfirba's avatar
Level 50

@jimmck I don't mind writing my own wrapper around some other PHP classes. That's quite a big class, will have to learn how it works.

Do you know any image to PDF class?

isaackearl's avatar

@kfirba to do the image to pdf conversion, you probably will have to use an html to pdf php library that supports images and then do some sort of image embedded in html to convert to pdf.

kfirba's avatar
Level 50

@isaackearl I've actually just found a solution, that was pretty easy tbh:

$file = storage_path('test.jpg');
$image = new Imagick($file);

$image->setImageFormat('pdf');

$image->writeImage(storage_path('test.pdf'));

return "Done";

Just before we are using this, we will need to install some packages:

sudo apt-get install libmagickwand-dev imagemagick php5-imagick
sudo service nginx restart
sudo service php5-fpm restart

Now I just need to find a way to convert doc/docx file :x

kfirba's avatar
kfirba
OP
Best Answer
Level 50

@isaackearl @jimmck I wanted to keep you updated, I've actually find a fine solution for my issue.

Convert Images to PDF

Before we can use the PHP library, we will need to install few dependencies:

sudo apt-get install libmagickwand-dev imagemagick php5-imagick
sudo service nginx restart
sudo service php5-fpm restart

Now we can simply use it like so:

$file = storage_path('test.jpg');
$image = new Imagick($file);

$image->setImageFormat('pdf');

$image->writeImage(storage_path('test.pdf'));

return "Done";

Convert doc, docx, xls, txt etc. to PDF

In order to do that, we will use libreoffice which can do that for us with no effort at all. We will install the most basic installation of libreoffice as we only need the command line:

sudo apt-get install libreoffice --no-install-recommends
sudo apt-get install libreoffice-calc # <- this one if for .xls files

It will install around 400MB of data on your server just so you know.

Now, when we want to convert a file to PDF:

libreoffice --headless --invisible --convert-to pdf output.pdf input.docx

We can combine the command above with Symfony Process object or just call it with exec method. I would recommend put all converting in a queue so the user won't need to wait until it's done.

2 likes
_chris's avatar

@kfirba I am also using libreoffice to convert a docx to pdf but am having some issues with the path and was wondering whether you also found this?

I am trying to save the converted pdf to the storage path but it keeps going into the public folder for some reason. Even though the docx is being read from the storage path (and also saved to the storage path using phpword).

libreoffice --headless --invisible --convert-to pdf /home/vagrant/Code/homestead.app/storage/reports/report.pdf /home/vagrant/Code/desuto.app/storage/reports/report.docx

Any ideas?

SOLVED: Just had to change directory in the storage path first.

chdir(storage_path().'/reports');
ThePlatinum's avatar

Late to the party, but, here is a solution I found to be cleaner using DomPDF.

composer require barryvdh/laravel-dompdf

Then create a view:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Certificate</title>
    <style>
        img {
            width: auto;
            height: 100%;
        }
    </style>
</head>

<body>
    <img src="<?php echo $_SERVER["DOCUMENT_ROOT"] . $cert_url; ?>" alt="Certificate">
</body>
</html>

Then create a function in a controller for the download link:

public function getPdf(){
			$pdf = Pdf::loadView('certificatepdf', [
            'cert_url' => $course->certificate_image_url
        ]);

        return $pdf
            ->setOption(['dpi' => 150])
            ->setPaper('a4', 'landscape')
            ->download($pdfname);
}

Please or to participate in this conversation.