phpexcel使用具有pdf宽度问题的tcpdf保存pdf
Posted
技术标签:
【中文标题】phpexcel使用具有pdf宽度问题的tcpdf保存pdf【英文标题】:phpexcel save pdf using tcpdf having pdf width issue 【发布时间】:2018-02-01 08:11:57 【问题描述】:我正在使用 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;
【问题讨论】:
【参考方案1】:尝试设置纵向可视化:
$objPHPExcel->getActiveSheet()
->getPageSetup()
->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
【讨论】:
【参考方案2】:我遇到了同样的问题,我不得不停用列的 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 打印如下:
答案可能有点晚,但我希望它对您将来有所帮助。
【讨论】:
以上是关于phpexcel使用具有pdf宽度问题的tcpdf保存pdf的主要内容,如果未能解决你的问题,请参考以下文章