Convert URL's to PDF or Image File With PHP

Do you need a PDF or Image convertor on your website? You can convert URL's, HTML Codes to PDF File with PHP.

Jun 6, 2023 - 14:49
Jun 6, 2023 - 16:33
 0
Convert URL's to PDF or Image File With PHP

Requirements:
1-
Active Google Chrome Installation
2- Composer

Google Chrome Paths:
- Windows:
C:\Program Files\Google\Chrome\Application\chrome
- MacOS:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
- Linux:
/usr/bin/google-chrome

COMPOSER PACKAGE INSTALLATION

Composer Installation PDF Convert with PHP Step 1

1-
Create a directory. (We created a directory in D:\SERVER\www with the name "pdf-creator"
2 -
Open the search menu and type: CMD. Right Click to Command Prompt and click to Run as Administrator
3- Type D: and press Enter key.
4- Type cd pdf-creator path and press Enter. (cd D:\SERVER\www\pdf-creator)
5- Type name of the composer package:

composer require daandesmedt/phpheadlesschrome

6- If you see "No composer.json in current directory", type n and press Enter.
7- Wait for the package installation to finish.



Web Page (URL) Convert to PDF with PHP

1- Create a file in your Package Path. We created index.php in D:\SERVER\www\pdf-convertor

require_once "vendor/autoload.php';

use daandesmedt\PHPHeadlessChrome\HeadlessChrome;

$headlessChromer = new HeadlessChrome();
$headlessChromer->setUrl('URL'); // Type the URL you want to convert to PDF file.
$headlessChromer->setBinaryPath('CHROMEPATH'); // Change CHROMEPATH with your Chrome Path
$headlessChromer->setOutputDirectory(__DIR__); // PDF file will be created in the same directory. You can change this.
$headlessChromer->toPDF('output.pdf'); // file name is output.

echo 'PDF generated to : ' . $headlessChromer->getFilePath();

Your URL Content converted to PDF.



Paramaters:

Use Mobile Mode:

$headlessChromer->useMobile();

Disable Header and Footer Texts:

$headlessChromer->disablePDFHeader();

 

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow