如何用tcpdf旋转整页?

时间:2015-07-13 08:55:21

标签: php pdf tcpdf

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

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

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

基本上:

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

所以我复制了那种格式,为Landscape创建了一个,为Portrait创建了一个。

然后根据$ 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 ../
    );