phpexcel使用具有pdf宽度问题的tcpdf保存pdf

时间:2018-02-01 08:11:58

标签: php phpexcel tcpdf mpdf

我使用php excel tcpdf渲染将文件另存为pdf。 pdf可以成功保存,但pdf只保存为A4大小,它会削减我的列。我有很多列,所以我需要pdf上的所有列。 pdf宽度应该是自动的,因此它可以根据给定的列数生成pdf。请看下面是我试过的代码。我也包括我从pdf生成的图像。你可以看到我的pdf宽度已修复,所以我的内容被删除了。任何建议

    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');


    $rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;

    $rendererLibrary = 'tcpdf';

    $rendererLibraryPath = '' . $rendererLibrary;

    //echo $rendererLibraryPath;
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();


    // Set document properties
    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                                 ->setLastModifiedBy("Maarten Balliauw")
                                 ->setTitle("PDF Test Document")
                                 ->setSubject("PDF Test Document")
                                 ->setDescription("Test document for PDF, generated using PHP classes.")
                                 ->setKeywords("pdf php")
                                 ->setCategory("Test result file");

    // Rename worksheet
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    $objPHPExcel->getActiveSheet()->setShowGridLines(false);

     //If used external excel file 
    $inputFile="xlsx_file/subcategory.xlsx";
    $inputFileType = PHPExcel_IOFactory::identify($inputFile);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFile);

    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
    {



        $worksheet->setShowGridLines(true);
        $worksheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER );
        $worksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER );
        $worksheet->getPageSetup()->setFitToPage(true);
        $worksheet->getPageSetup()->setFitToWidth(1);
        $worksheet->getPageSetup()->setFitToHeight(0);


    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);



    if (!PHPExcel_Settings::setPdfRenderer(
            $rendererName,
            $rendererLibraryPath
        )) {
        die(
            'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
            '<br />' .
            'at the top of this script as appropriate for your directory structure'
        );
    }

    $rand = rand(1234, 9898);
    $presentDate = date('YmdHis');
    $fileName = "report_" . $rand . "_" . $presentDate . ".pdf";

    // Redirect output to a client’s web browser (PDF)
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment;filename="'.$fileName.'"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
    $objWriter->save('php://output');
    exit;

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试设置纵向可视化:

$objPHPExcel->getActiveSheet()
            ->getPageSetup()
            ->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);

答案 1 :(得分:0)

我遇到了同样的问题,我不得不停用列的autoSize并手动设置宽度,如下所示:

 $objPHPExcel ->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('B')->setAutoSize(false);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('D')->setAutoSize(false);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('E')->setAutoSize(false);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('F')->setAutoSize(false);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('G')->setAutoSize(false);

        $objPHPExcel ->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('C')->setWidth(14);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('E')->setWidth(14);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('F')->setWidth(18);
        $objPHPExcel ->getActiveSheet()->getColumnDimension('G')->setWidth(9);

这样做之后,我的pdf如下打印: enter image description here

答案可能有点晚,但是我希望它对以后有帮助。