在PHP网页上实现“以PDF格式下载”

时间:2022-10-12 18:32:55

What are the range of options available for implementing a "Download as PDF" feature on a PHP-based web page? Do I need to implement a server-side PHP-based PDF generator using a third party library, or are external web services available that can accomplish this for me?

在基于php的web页面上实现“下载为PDF”功能的选项有哪些?我是否需要使用第三方库实现服务器端基于php的PDF生成器,或者是否有外部web服务可以为我实现这一点?

4 个解决方案

#1


2  

Use TCPDF over fpdf, because:

使用TCPDF / fpdf,因为:

  • a) it supports unicode
  • 它支持unicode
  • b) it's still being actively maintained
  • b)仍在积极维护
  • c) is based on a more modern OO architecture.
  • c)基于更现代的OO架构。

By way of trivia, TCPDF was based upon fpdf many years ago, but was almost entirely remastered since. It still comes with source code and is highly adviseable.

顺便提一下,TCPDF是多年前基于fpdf的,但此后几乎完全被重新掌握。它仍然带有源代码,并且是非常明智的。

Example usage is another question, and a practive of extending the base class, overriding methods, etc. Don't worry, the download contains lots of samples (10 or more projects) from simple to advanced. I am experienced in PHP, but even taking that into account, some years back, I was able to ship our first prototype of localized pdf reports in all of one day.

示例使用是另一个问题,它是扩展基类、重写方法等的实际操作。我在PHP方面很有经验,但即使考虑到这一点,几年前,我还是能够在一天之内发布我们的第一个本地化pdf报告原型。

class MyPDF extends TCPDF {
   // meaningful code
}

By the way, there's even a TCPDF tag here on * :)

顺便说一下,在*上甚至还有一个TCPDF标签:)

#2


2  

Note: I'm assuming you want to know how to generate PDF files dynamically with PHP

注意:我假设您想知道如何使用PHP动态生成PDF文件

I recommend fpdf. The site seems to be down right now, but you can probably download it elsewhere.

我建议fpdf。这个网站现在似乎已经关闭了,但是你可以从其他地方下载。

To answer your question directly, your best option is indeed to use a third-party library. Of course a PDF file is nothing but a normal file that follows the specifications laid out for 'PDF files', so you could always roll your own.

要直接回答您的问题,您最好的选择确实是使用第三方库。当然,PDF文件只是一个普通的文件,它遵循了“PDF文件”的规范,所以您可以一直使用自己的文件。

But that's a waste of time, given what's already available freely for pdf generation. So you should find a library/class that can do it for you (again, I recommend fpdf but I'm sure there's many other libraries out there that can do this too, so look around before you pick one).

但是这是浪费时间,因为pdf生成是免费的。所以你应该找一个库/类来做这个(同样,我推荐fpdf,但是我确信还有很多其他库也可以做这个,所以在你选择一个库之前先看看)。

Edit: I should add that it's been a while since I've used fpdf, and zanlok seems to have indicated that it's no longer maintained, so maybe look for another library. But definitely look for one; don't roll your own!

编辑:我应该补充一点,我使用fpdf已经有一段时间了,而赞洛克似乎已经表明它不再被维护了,所以也许可以寻找另一个库。但一定要找一个;不要使用你自己的!

#3


1  

If you are able to install programs on your web server: http://code.google.com/p/wkhtmltopdf/

如果您能够在您的web服务器上安装程序:http://code.google.com/p/wkhtmltopdf/。

It's very easy to generate high quality PDFs. For example:

很容易生成高质量的pdf文件。例如:

wkhtmltopdf http://www.google.com/ google.pdf

It will do exactly what you expect.

它会按照你的预期做。

#4


0  

Mpdf is an excellent library for this. You can use custom fonts, control page breaks and all manner of formatting. Different methods of accepting input. It's flexible and solid (though it does not yet handle brand new methods such as CSS Flexbox).

Mpdf是一个很好的库。您可以使用自定义字体、控制分页符和各种格式。接受输入的不同方法。它灵活而坚固(尽管它还没有处理像CSS Flexbox这样的全新方法)。

https://mpdf.github.io/

https://mpdf.github.io/

#1


2  

Use TCPDF over fpdf, because:

使用TCPDF / fpdf,因为:

  • a) it supports unicode
  • 它支持unicode
  • b) it's still being actively maintained
  • b)仍在积极维护
  • c) is based on a more modern OO architecture.
  • c)基于更现代的OO架构。

By way of trivia, TCPDF was based upon fpdf many years ago, but was almost entirely remastered since. It still comes with source code and is highly adviseable.

顺便提一下,TCPDF是多年前基于fpdf的,但此后几乎完全被重新掌握。它仍然带有源代码,并且是非常明智的。

Example usage is another question, and a practive of extending the base class, overriding methods, etc. Don't worry, the download contains lots of samples (10 or more projects) from simple to advanced. I am experienced in PHP, but even taking that into account, some years back, I was able to ship our first prototype of localized pdf reports in all of one day.

示例使用是另一个问题,它是扩展基类、重写方法等的实际操作。我在PHP方面很有经验,但即使考虑到这一点,几年前,我还是能够在一天之内发布我们的第一个本地化pdf报告原型。

class MyPDF extends TCPDF {
   // meaningful code
}

By the way, there's even a TCPDF tag here on * :)

顺便说一下,在*上甚至还有一个TCPDF标签:)

#2


2  

Note: I'm assuming you want to know how to generate PDF files dynamically with PHP

注意:我假设您想知道如何使用PHP动态生成PDF文件

I recommend fpdf. The site seems to be down right now, but you can probably download it elsewhere.

我建议fpdf。这个网站现在似乎已经关闭了,但是你可以从其他地方下载。

To answer your question directly, your best option is indeed to use a third-party library. Of course a PDF file is nothing but a normal file that follows the specifications laid out for 'PDF files', so you could always roll your own.

要直接回答您的问题,您最好的选择确实是使用第三方库。当然,PDF文件只是一个普通的文件,它遵循了“PDF文件”的规范,所以您可以一直使用自己的文件。

But that's a waste of time, given what's already available freely for pdf generation. So you should find a library/class that can do it for you (again, I recommend fpdf but I'm sure there's many other libraries out there that can do this too, so look around before you pick one).

但是这是浪费时间,因为pdf生成是免费的。所以你应该找一个库/类来做这个(同样,我推荐fpdf,但是我确信还有很多其他库也可以做这个,所以在你选择一个库之前先看看)。

Edit: I should add that it's been a while since I've used fpdf, and zanlok seems to have indicated that it's no longer maintained, so maybe look for another library. But definitely look for one; don't roll your own!

编辑:我应该补充一点,我使用fpdf已经有一段时间了,而赞洛克似乎已经表明它不再被维护了,所以也许可以寻找另一个库。但一定要找一个;不要使用你自己的!

#3


1  

If you are able to install programs on your web server: http://code.google.com/p/wkhtmltopdf/

如果您能够在您的web服务器上安装程序:http://code.google.com/p/wkhtmltopdf/。

It's very easy to generate high quality PDFs. For example:

很容易生成高质量的pdf文件。例如:

wkhtmltopdf http://www.google.com/ google.pdf

It will do exactly what you expect.

它会按照你的预期做。

#4


0  

Mpdf is an excellent library for this. You can use custom fonts, control page breaks and all manner of formatting. Different methods of accepting input. It's flexible and solid (though it does not yet handle brand new methods such as CSS Flexbox).

Mpdf是一个很好的库。您可以使用自定义字体、控制分页符和各种格式。接受输入的不同方法。它灵活而坚固(尽管它还没有处理像CSS Flexbox这样的全新方法)。

https://mpdf.github.io/

https://mpdf.github.io/