TCPDF Custom page size

前端 未结 7 1539
执笔经年
执笔经年 2020-12-09 08:35

This example on tcpdf\'s website shows how to use page formats like A4, A5 etc, but how do I set tcpdf to use custom sizes like 175mm x 266 mm?

Solutions appreciated.

相关标签:
7条回答
  • 2020-12-09 09:00

    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).
    • Alternatively, you can define advanced page details (MediaBox, Cropbox, BleedBox, TrimBox, ArtBox) as explained on the documentation of the setPageFormat() method at http://www.tcpdf.org.

    Check also the default examples no. 28 and 60 at http://www.tcpdf.org.

    0 讨论(0)
  • 2020-12-09 09:06

    The above answer won't work for me, so I add my solution here - from http://www.tcpdf.org/examples/example_060.phps, change urx, ury for your purpose

    // set page format (read source code documentation for further information)
    // MediaBox - width = urx - llx 210 (mm), height = ury - lly = 297 (mm) this is A4
    $page_format = array(
        'MediaBox' => array ('llx' => 0, 'lly' => 0, 'urx' => 210, 'ury' => 297),
        //'CropBox' => array ('llx' => 0, 'lly' => 0, 'urx' => 210, 'ury' => 297),
        //'BleedBox' => array ('llx' => 5, 'lly' => 5, 'urx' => 205, 'ury' => 292),
        //'TrimBox' => array ('llx' => 10, 'lly' => 10, 'urx' => 200, 'ury' => 287),
        //'ArtBox' => array ('llx' => 15, 'lly' => 15, 'urx' => 195, 'ury' => 282),
        'Dur' => 3,
        'trans' => array(
            'D' => 1.5,
            'S' => 'Split',
            'Dm' => 'V',
            'M' => 'O'
        ),
        'Rotate' => 90,
        'PZ' => 1,
    );
    
    // Check the example n. 29 for viewer preferences
    
    // add first page ---
    $pdf->AddPage('P', $page_format, false, false);
    
    0 讨论(0)
  • 2020-12-09 09:07

    The truth, now you can solve it like this.

    //AddPage [P(PORTRAIT),L(LANDSCAPE)],FORMAT(A4-A5-ETC)
    
    $pdf->AddPage('P','A5');
    

    Source: https://tcpdf.org/examples/example_028/

    0 讨论(0)
  • 2020-12-09 09:19

    No editing of the class is require... tcpdf doesn't accept a width/length parameter, it just accepts two lengths and determines which is which using the layout (either Portrait or Landscape)

    $pageLayout = array($width, $height); //  or array($height, $width) 
    $pdf = new TCPDF('p', 'pt', $pageLayout, true, 'UTF-8', false);
    
    0 讨论(0)
  • 2020-12-09 09:24

    Go to /config/tcpdf_config.php and around line 117, modify the line:

    define ('PDF_PAGE_FORMAT', 'A4');
    

    by

    define ('PDF_PAGE_FORMAT', 'LETTER');
    

    It is important to put "LETTER" in uppercase, you can see all possible values in this file: tcpdf/include/tcpdf_static.php.

    0 讨论(0)
  • 2020-12-09 09:25

    EDIT : I was just wrong : you can give an array (array($width, $height)) in parameter..

    I created a tcpdf subclass where I modified a few things : getPageSizeFromFormat(); Here is the code : http://paste.pocoo.org/show/294958/.

    Then I call my custom class, add a new format and set a new format :

    $pdf = new CUSTOMPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
    //Add a custom size  
    $width = 175;  
    $height = 266; 
    $orientation = ($height>$width) ? 'P' : 'L';  
    $pdf->addFormat("custom", $width, $height);  
    $pdf->reFormat("custom", $orientation);  
    
    0 讨论(0)
提交回复
热议问题