How to set custom page width and height in TCPDF?

I use TCPDF to create a PDF file from HTML content. I want to set the width and height of the page to custom values ​​of 400px and 300px.

I used the following code

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

Where

  • PDF_PAGE_ORIENTATION - p ;
  • PDF_UNIT - mm ;
  • PDF_PAGE_FORMAT A6 .
+6
source share
3 answers

You can do:

 $custom_layout = array($your_width, $your_height); $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, $custom_layout, true, 'UTF-8', false); 
+12
source

Instead of PDF_PAGE_FORMAT (or A6), you can use an array to specify the width and height.

The third argument to the TCPDF constructor takes either a string like "A4", "A6", etc., or a two-element array containing the width and height (in units defined in PDF_UNIT).

So basically:

 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, array(400, 300), true, 'UTF-8', false); 
+6
source

If you want to define a new page by name (a fully custom page that has nothing to do with it), then here's how to do it:

  • Go to /tcpdf/include/tcpdf_static.php
  • Scroll to line 478
  • Add personalized sheets in the same format

     public static function getPageSizeFromFormat($format) { switch (strtoupper($format)) { // ISO 216 A Series + 2 SIS 014711 extensions case 'A0' : {$pf = array( 2383.937, 3370.394); break;} case 'A1' : {$pf = array( 1683.780, 2383.937); break;} case 'A2' : {$pf = array( 1190.551, 1683.780); break;} case 'A3' : {$pf = array( 841.890, 1190.551); break;} case 'A4' : {$pf = array( 595.276, 841.890); break;} case 'A5' : {$pf = array( 419.528, 595.276); break;} case 'A6' : {$pf = array( 297.638, 419.528); break;} 

Just add another case with your custom size.

  case 'SQUARE' : {$pf = array( 297.638, 297.638); break;} case 'SMALLSQUARE' : {$pf = array( 100.00, 100.00); break;} 

Then use the regular constructor to invoke the new page:

 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'SQUARE', true, 'UTF-8', false); 
+3
source

Source: https://habr.com/ru/post/954248/


All Articles