简体   繁体   English

如何在TCPDF中设置自定义页面宽度和高度?

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

I am using TCPDF to generate a PDF file from HTML content. 我正在使用TCPDF从HTML内容生成PDF文件。 I want to set the page width and height to the custom values 400px and 300px. 我想将页面宽度和高度设置为自定义值400px和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_PAGE_ORIENTATIONp ;
  • PDF_UNIT is mm ; PDF_UNITmm ;
  • PDF_PAGE_FORMAT is A6 . PDF_PAGE_FORMATA6

you could do: 你可以这样做:

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

Instead of PDF_PAGE_FORMAT (or A6), you could use an array to specifiy width and height. 您可以使用数组来指定宽度和高度,而不是PDF_PAGE_FORMAT(或A6)。

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). TCPDF构造函数的第三个参数接受像'A4','A6'等字符串或包含宽度和高度的双元素数组(以PDF_UNIT中定义的单位)。

So basically: 所以基本上:

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

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 转到/tcpdf/include/tcpdf_static.php
  2. Scroll to Line 478 滚动到第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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM