【问题标题】:How to rotate full page with tcpdf?如何使用 tcpdf 旋转整页?
【发布时间】:2015-07-13 08:55:21
【问题描述】:

我正在使用 TCPDF 库编写带有背景图像和多个文本块的自定义尺寸标签。

用户在看到 PDF 屏幕上的预览时,它应该水平显示,但对于打印,我需要将整页旋转 -90 度。

如何在不移动任何内容的情况下旋转整个页面以打印版本?

【问题讨论】:

    标签: php pdf tcpdf


    【解决方案1】:

    基本上:

    在我的情况下,我已经不得不为我的文档所需的特殊尺寸使用一种新的文档格式。

    所以我复制了该格式,为横向创建了一种,为纵向创建了一种。

    然后基于 $preview 变量,如果预览,我正在渲染正常的横向文档,但如果不预览,我将使用纵向格式和方向,并开始转换和旋转页面上的所有内容。

    希望这对我没有找到其他“快速”方法来完成这种整页轮换的人有所帮助。

    <?php
    
    // #1 get the preview attribute from 
    // the form that was submitted from the user
    $preview=  isset($_POST['preview'])?(int)$_POST['preview']:0;
    
    // load TCPDF for CodeIgniter as a library
    $this->load->library('Pdf');
    
    // #2 set default orientation and format
    $orientation='L';
    $format='MAKE-L';
    
    // #3 if not previewing, switch orientation and format to portrait
    if (!$preview) {
      $orientation='P';
      $format='MAKE-P';
    }
    
    // create new pdf object 
    // (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
    $pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
    
    // remove default header/footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    
    $pdf->SetMargins(0, 0, 0);
    
    $pdf->AddPage($orientation, $format);
    
    // #4 if not previewing, start transformation 
    // and rotate everything before inserting any content
    if (!$preview) {
    
      // Start Transformation
      $pdf->StartTransform();
    
      // Rotate counter-clockwise centered by (x,y)
      $pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
    
    }
    
    // put your content here, 
    // for example set font and add a text
    $pdf->SetFont('times', '', 7, '', true);
    $pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
    /// end content
    
    // #5 if not in preview mode, finish the transformation
    if (!$preview) {
      // Stop Transformation
      $pdf->StopTransform();
    }
    
    $pdf->Output('example.pdf', 'I');
    
    /**
     * Last but very important note:
     * I have added my formats in tcpdf/includes/tcpdf_static.php file.
     * >> MAKE-L for Landscape
     * >> MAKE-P for Portrait
     */
    
     public static $page_formats = array(
    
            // Make
            'MAKE-L'                     => array( 396.850,  425.196), // = (  h 140 x w 150 ) mm
    
            // Make
            'MAKE-P'                     => array( 425.196, 396.850  ), // = (  h 140 x w 150 ) mm
    
            // .. rest of formats here ../
        );
    

    【讨论】:

      【解决方案2】:

      setPageFormat() 方法应该可以完成这项工作。也可以将参数传递给AddPage()的$format参数:

      $pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-30
        • 2020-04-18
        • 2015-04-14
        • 1970-01-01
        • 1970-01-01
        • 2019-12-10
        • 2019-07-04
        • 1970-01-01
        相关资源
        最近更新 更多