Wednesday, November 29, 2023
HomeFun With ProgrammingConvert HTML to PNG in Laravel with TCPDF and ImageMagick

Convert HTML to PNG in Laravel with TCPDF and ImageMagick

In this tutorial, we are going to use the TCPDF package to Convert HTML to PNG in Laravel.
Here notice that TCPDF is primarily made for PDF generation, not for image generation, so it cannot directly convert HTML to PNG. However, you can use a combination of TCPDF and ImageMagick to achieve the conversion.

The TCPDF is a class used for generating PDF files. The constructor of the TCPDF class takes various parameters like page orientation, a unit of measurement, page format, and a few other optional parameters such as header and footer functions, and font subsetting.

$pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

Here is an explanation of each parameter:

L: It is the orientation, which stands for landscape. Alternatively, the value can be ‘P’ for a portrait.
PDF_UNIT: It is the unit of measurement used for the PDF document, and its default value is millimeters.
PDF_PAGE_FORMAT: It is the page format of the PDF document. The default value is A4.
true: It specifies whether to include Unicode font support or not.
UTF-8: It is the default charset for the document.
false: It specifies whether to enable or disable core fonts such as Helvetica, Courier, and Times.

Here’s an example of how you can convert HTML to PNG using TCPDF and ImageMagick in Laravel:

  1. Install the TCPDF and ImageMagick packages using Composer:
composer require tecnickcom/tcpdf
composer require imagick/imagick
  1. Add the following line to the providers array in your config/app.php file:
'providers' => [
    // ...
    Imagick\ImagickServiceProvider::class,
    // ...
],
  1. Add the following line to the aliases array in your config/app.php file:
'aliases' => [
    // ...
    'Imagick' => Imagick\Facades\Imagick::class,
    // ...
],
  1. Now you can use the following code to convert HTML to PNG using TCPDF and ImageMagick:
use TCPDF;
use Imagick;

public function convertHtmlToPng()
{
    // Generate HTML code
    $html = '<html><body>Hello, world!</body></html>';

    // Create TCPDF object
    $pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // Set PDF margins and auto page breaks
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // Add HTML content to PDF
    $pdf->writeHTML($html, true, false, true, false, '');

    // Get PDF as string
    // output() second parameter is destination where to send the document. It can take one of the following values:
    // I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the β€œSave as” option on the link generating the PDF.
    // D: send to the browser and force a file download with the name given by name.
    // F: save to a local server file with the name given by name.
    // S: return the document as a string. name is ignored.
    // FI: equivalent to F + I option
    // FD: equivalent to F + D option
    $pdfData = $pdf->Output('', 'S');

    // Create Imagick object
    $image = new Imagick();

    // Read PDF data into Imagick object
    $image->readImageBlob($pdfData);

    // Set image format and quality
    $image->setImageFormat('png');
    $image->setImageCompressionQuality(100);

    // Save image to file or output to browser
    $image->writeImage('output.png');
}

In this example, we first generate the HTML code that we want to convert to PNG. We then create a TCPDF object and add the HTML content to it using the writeHTML method. We then get the PDF data as a string using the Output method.

Next, we create an Imagick object and read the PDF data into it using the readImageBlob method. We then set the image format and quality using the setImageFormat and setImageCompressionQuality methods, respectively.

Finally, we save the image to a file using the writeImage method. Alternatively, we can output the image directly to the browser using the echo $image statement.

Note that this example requires the Imagick extension to be installed on your server.

That’s it for today friends, if you like this article then please like, share this with your circle, and show some β€οΈ.

Thank You!😊 Keep supporting us.

Read More:

Rahul K Raj
Rahul K Rajhttps://rkrknowledge.com/
Hi πŸ‘‹, I'm Rahul Raj Kushwaha also known as Rahul K Raj. A passionate full-Stack developer from India. You can call me: Entrepreneur | Youtuber | Blogger | Coder | Developer | UI Designer | Hacker | Music Composer. I like to share my knowledge on such great platforms like RKR Knowledge. I try to learn something new every day.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Stay Connected

33FansLike
60FollowersFollow
520SubscribersSubscribe

Most Popular

Recent Comments