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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18915349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:28:07  来源:igfitidea点击:

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

phptcpdf

提问by user2000375

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.

我正在使用 TCPDF 从 HTML 内容生成 PDF 文件。我想将页面宽度和高度设置为自定义值 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_ORIENTATIONis p;
  • PDF_UNITis mm;
  • PDF_PAGE_FORMATis A6.
  • PDF_PAGE_ORIENTATIONp
  • PDF_UNITmm
  • PDF_PAGE_FORMATA6

回答by Sudhir Bastakoti

you could do:

你可以这样做:

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

回答by Davide De Santis

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

回答by Tucker

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;}
    
  1. 转到/tcpdf/include/tcpdf_static.php
  2. 滚动到第 478 行
  3. 以相同的格式添加自定义尺寸的工作表

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