Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Converting a word file to PDF with proper formatting is not easy. I tried many packages in Laravel but they all gave me broken PDFs with messed up styles.
I found an API called CloudConvert(https://cloudconvert.com/) that supports the conversion between more than 200 different audio, video, document, ebook, archive, image, spreadsheet and presentation formats. This helped me to convert the doc file to PDF format very easily without breaking any style.
It’s very easy to install and use in Laravel via composer.js
"require": {"robbiep/cloudconvert-laravel": "2.*"}
Run the composer install to download the required files.
You need to publish the config PHP artisan vendor:publish
Just enter your API key in config/cloudconvert.php
Note: You will get an API key from your CloudConvert registered profile (Dashboard -> Authorization).
Usage
There are many ways to use CloudConvert. I’ll cover a few of them here.
1 2 | /* Dynamic PDF creation using DOCX. */ CloudConvert::file('invoice_template.docx')->to('invoice.pdf'); |
This will do the trick. Here, the invoice_template.docx file will get converted as invoice.pdf file.
It will also work with converting remote files (just make sure you provide a path to save it to).
1 2 | /* Convert Google's SVG logo hosted on Wikipedia to a png on your server */ CloudConvert::file('http://upload.wikimedia.org/wikipedia/commons/a/aa/Logo_Google_2013_Official.svg')->to('images/google.png'); |
Merge the PDFs
CloudConvert also helps to merge multiple files to a specific format. Right now, merging only works with remotely hosted files
1 2 3 4 | CloudConvert::merge([ 'https://cloudconvert.com/assets/d04a9878/testfiles/pdfexample1.pdf', 'https://cloudconvert.com/assets/d04a9878/testfiles/pdfexample2.pdf' ])->to('merged.pdf'); |
Website screenshot
CloudConvert will also take a screenshot of a website and convert it into an image or pdf for you.
1 2 | /* Take a screenshot with the default options: 1024px with with full height of webpage */ CloudConvert::website('www.nyan.cat')->to('screenshots/nyan.jpg'); |
1 2 3 4 | /* You can also specify the width and the height as converter options */ CloudConvert::website('www.nyan.cat') ->withOptions(['screen_width' => 1024,'screen_height' => 700]) ->to('screenshots/nyan.png'); |
CloudConvert (including API) can be used absolutely free! However, free accounts have 25 only conversion minutes per day.
What is a conversion minute?
The longer a conversion takes, the more resources it consumes and the more expensive it is. Our prepaid packages are based on conversion minutes.
Every conversion takes at least one conversion minute. Normally, document or image conversions consume only one conversion minute. Video/Audio re-encodings or huge files consume more conversion minutes, depending on quality and codecs. Of course, only successful conversions are counted.
For more info and all the converter options, I suggest checking out the API docs, https://github.com/robbiepaul/cloudconvert-laravel.