How to generate PDF of HTML code in Drupal 8 | Valuebound

Page 1

How to generate PDF of HTML code in Drupal 8 Have you ever been in a situation where you were required to generate PDF of HTML code? Recently, I came across a similar situation. In Drupal 8, there are several PHP libraries that allows you to generate PDF. However, out of curiosity, I thought of finding better alternatives. After some research, I found an interesting library, called mPDF, that converts HTML code to PDF without changing the structure.

mPDF is a smart library that considers the CSS attached to the HTML. Not only CSS, it takes care of almost all the HTML tags like form tags, tables, images, lists etc.

Generating PDF of HTML in custom module Requirements mPDF 7.0 requires PHP ^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0. PHP mbstring and gd extensions. Installation Step 1 Download mPDF via composer and its packagist package mpdf/mpdf. Step 2 Run the following command in Drupal root directory: $ composer require mpdf/mpdf

Step 3 Once the mpdf libraries pdf installs, generate the pdf in custom module Step 4 In custom module, create a controller with routing path(/pdf-download) $html = ‘this is my <b>first</b>downloadable pdf’; $mpdf = new \Mpdf\Mpdf(['tempDir' => 'sites/default/files/tmp']); $mpdf>WriteHTML($html);


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
How to generate PDF of HTML code in Drupal 8 | Valuebound by Alaska william - Issuu