user2000375
user2000375

Reputation: 71

How can I set custom page width and height in 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

Upvotes: 7

Views: 48697

Answers (3)

Tucker
Tucker

Reputation: 129

If you want to define a new page by name (a completely custom page that has no relation), then this is how:

  1. Go to /tcpdf/include/tcpdf_static.php
  2. Scroll to Line 478
  3. Add your custom sized 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 normal constructor to call the new page:

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

Upvotes: 3

Davide De Santis
Davide De Santis

Reputation: 1034

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).

So basically:

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

Upvotes: 8

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

you could do:

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

Upvotes: 12

Related Questions