Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set custom page width and height in TCPDF?

Tags:

php

tcpdf

I am using TCPDF to generate a PDF file from HTML content. I want to set the page width and height to the custom values 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 is p;
  • PDF_UNIT is mm;
  • PDF_PAGE_FORMAT is A6.
like image 322
user2000375 Avatar asked Sep 20 '13 11:09

user2000375


People also ask

How to define the page size in TCPDF?

On the newer TCPDF version you can define the page size in multiple ways: All standard page formats are already defined (more than 300 types). You can simply define a page size by defining an array with 2 numbers: width, height (regardless the page orientation).

What's the best way to set the page height and width?

For a responsive full page height, set the body element min-height to 100vh. If you set a page width, choose 100% over 100vw to avoid surprise horizontal scrollbars. I'll leave you with a tutorial from my YouTube channel demonstrating the CSS height and width settings for an HTML page that is full screen size and grows with the content it contains:

How to get the width and height of a PDF file?

Instead of PDF_PAGE_FORMAT (or A6), you could use an array to specifiy width and height. The 3rd argument of the TCPDF constructor accepts either a string like 'A4', 'A6', etc. or a two-element array containing the width and height (in the units defined in PDF_UNIT).

How do I define a standard page size?

All standard page formats are already defined (more than 300 types). You can simply define a page size by defining an array with 2 numbers: width, height (regardless the page orientation).


1 Answers

you could do:

$custom_layout = array($your_width, $your_height);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, $custom_layout, true, 'UTF-8', false);
like image 63
Sudhir Bastakoti Avatar answered Sep 18 '22 00:09

Sudhir Bastakoti