如何通过 PHP 将 .xls 转换为 .pdf?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23223491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:34:58  来源:igfitidea点击:

How to convert .xls to .pdf via PHP?

phppdfxls

提问by David Holada

I was googling many hours but I haven't got solution yet.

我在谷歌上搜索了很多小时,但我还没有得到解决方案。

I need to convert .xls file into .pdf file. How to do that? I have found this http://www.aspose.com/, but it seems, that it hasn't got PHP API, only .NET and JAVA.

我需要将 .xls 文件转换为 .pdf 文件。怎么做?我找到了这个http://www.aspose.com/,但它似乎没有 PHP API,只有 .NET 和 JAVA。

Thanks for every advice...

谢谢你的每一个建议...

回答by Kryten

PHPExcelwill do it directly (read XLS, output PDF), although the results are not very pretty.

PHPExcel会直接做(读取XLS,输出PDF),虽然结果不是很漂亮。

A better solution might be to use PHPExcel to read the XLS file, render it to HTML in your code & style it the way you like it, then use an HTML->PDF converter like DOMPDF(that's the only one I've tried; there are others) to convert to PDF.

更好的解决方案可能是使用 PHPExcel 读取 XLS 文件,在您的代码中将其渲染为 HTML 并按照您喜欢的方式设置样式,然后使用 HTML-> PDF 转换器,如DOMPDF(这是我唯一尝试过的;还有其他)转换为PDF。

Or, if you want to skip the HTML step, render it directly to PDF using one of the PHP PDF libraries out there - my personal favourite is FPDF.

或者,如果您想跳过 HTML 步骤,请使用现有的 PHP PDF 库之一将其直接呈现为 PDF - 我个人最喜欢的是FPDF

回答by JuKe

use headless libreoffice and a shell command shell_exec('C:\wamp64\www\phpexcel\lo\App\libreoffice\program\soffice.exe --headless --convert-to pdf:calc_pdf_Export --outdir C:\wamp64\www\phpexcel\o C:\wamp64\www\phpexcel\new.xlsx');

使用 headless libreoffice 和 shell 命令 shell_exec('C:\wamp64\www\phpexcel\lo\App\libreoffice\program\soffice.exe --headless --convert-to pdf:calc_pdf_Export --outdir C:\wamp64\www\phpexcel\o C:\wamp64\www\phpexcel\new.xlsx');

回答by Sandipan S

Anyone who is looking to do this in Ubuntu/linux using php -

任何希望使用 php 在 Ubuntu/linux 中执行此操作的人 -

Ubuntu comes with libre office installed default. Anyone can use the shell command to use the headless libre office for this.

Ubuntu 默认安装了 libre office。任何人都可以使用 shell 命令为此使用 headless libre office。

shell_exec('/usr/bin/libreoffice --headless --convert-to pdf:calc_pdf_Export --outdir /var/www/html/XlsToPdf/public_html/src/var/output /var/www/html/XlsToPdf/public_html/src/var/source/company-a.xlsx');

Hope it helps a others like me.

希望它可以帮助像我这样的其他人。

回答by manoj

  • my favourite is fpdf
  • 我最喜欢的是 fpdf

Method 1 :

方法一:

  • step 1 go to fpdf.org
  • step2 In left side having download button then click to save
  • step3 create index.php(paste below code) and save index.php to downloaded folder from the site of fpdf.org
  • 步骤 1 转到 fpdf.org
  • step2 在左侧有下载按钮然后点击保存
  • step3 创建 index.php(粘贴下面的代码)并将 index.php 保存到从 fpdf.org 网站下载的文件夹中
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

For Reference youtube link www.youtube.com/watch?v=QSrUWoTUmyo

参考 youtube 链接 www.youtube.com/watch?v=QSrUWoTUmyo

Method 2:

方法二:

using phpmyadmin store and retrieve data as pdf format

使用 phpmyadmin 存储和检索数据为 pdf 格式

  • step 1: first import your excel to phpmyadmin
  • step 2: retrieve data from phpmyadmin using php code using echo concept
  • step 3: using fpdf library to display retrieved data as pdf file
  • 第1步:首先将您的excel导入phpmyadmin
  • 第 2 步:使用 php 代码使用 echo 概念从 phpmyadmin 检索数据
  • 第 3 步:使用 fpdf 库将检索到的数据显示为 pdf 文件

This Video is very usefull refer this friends(Refer this youtube video as step by step its work) https://www.youtube.com/watch?v=c9twV7jkjJg

这个视频非常有用,推荐这个朋友(参考这个youtube视频作为一步一步的工作https://www.youtube.com/watch?v=c9twV7jkjJg

method 3:

方法三:

html to pdf

html转pdf

Refer this youtube video as step by step its workRefer this links friends its usefull for you... https://www.youtube.com/watch?v=Kid6Xrqf9-Y

参考此 YouTube 视频作为其工作的分步指南参考此链接朋友对您有用... https://www.youtube.com/watch?v=Kid6Xrqf9-Y

Thank you Friends...

谢谢各位朋友...