php - 在不破坏设计/样式的情况下在 PHPSpreadsheet 中从 excel 转换为 pdf

标签 php excel pdf-generation spreadsheet phpspreadsheet

我尝试将 PHPSpreadsheet 导出为 PDF。我所做的是将标题设置为:

header("Content-Disposition: attachment;filename=hello world.pdf");

$writer 是这样的:

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->save("php://output");

我的其余代码与导出为 XLSX 的代码相同。它似乎不喜欢样式命令,并且还导入没有文本的单元格。我如何在 Excel 的单元格中创建信息的示例如下所示:

$sheet->setCellValue('A16', $activitiesCount)->getStyle('A16');
$sheet->getStyle('A16')->getFill()->setFillType(Fill::FILL_SOLID);
$sheet->getStyle('A16')->getFill()->getStartColor()->setARGB('d9e6fc');

这是excel中的原件:

enter image description here

如果我直接从 excel 本身将其另存为 PDF,则该文件可以很好地导出。但是当我尝试用我的代码来做时,这似乎是结果:

enter image description here

如您所见,它变得非常困惑。这是否意味着我必须以另一种方式为 PDF 编程?我认为在它们之间进行更改会很容易。

PS:不要介意图片之间的名称差异。我在数据库中测试了一些东西,看看当名字很长时它是如何扩展的。它并不漂亮,让我告诉你。

最佳答案

这不是一个很好的答案,但如果您阅读 the documentation你会看到一条注释:

Please note that PDF file format has some limits regarding to styling cells, number formatting, ...

尝试使用 mPDF 以外的库可能会更幸运。来自同一页面:

The different libraries have different strengths and weaknesses. Some generate better formatted output than others, some are faster or use less memory than others, while some generate smaller .pdf files

关于php - 在不破坏设计/样式的情况下在 PHPSpreadsheet 中从 excel 转换为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52089552/

相关文章:

php - 如何在同一个 php 脚本中销毁两个不同的 session ?

fonts - 报表查看器导出为 pdf 不嵌入字体

php - 列出目录中的图像(01.png)和描述(01.txt)

php - preg_replace 用于以冒号结尾的单词

vba 代码在 f8 中运行,但不在 f5 中

php - 在php中从mysql数据库下载excel表

javascript - PDF.JS 在客户端预览从服务器端发送的大型 pdf 文件(> 200MB)

css - ABCPdf 没有嵌入新添加的自定义字体

php - Laravel 自定义路由模型绑定(bind)

excel - 如何使用查找功能激活找到的单元格?