Converting HTML to PDF (not PDF to HTML) using PHP [closed]

为君一笑 提交于 2019-11-27 01:20:29

Regarding wkhtmltopdf:

  • This thing works blazingly fast and it can also handle all kinds of HTML/CSS you throw at it, so when you need speed, you should seriosly consider it. We switch to it recently in our company and our PDF serving got enourmous speed-boost.

  • At least under Linux it needs XOrg libraries to be installed - servers usually don't have them, so that might be your problem.

Try this:

Have you tried Prince?

But what if You will use any online service and send Your HTML content over HTTP? Of course most of them are not free.

One possibility: having the script automatically:

  1. Take the web page
  2. Open that page in a web browser
  3. Take a screencap of that page
  4. Turn it into a PDF

step 4 is easy - there are plenty of PHP/cmdline libraries that will let you put images onto a pdf or convert them (eg, fpdf.)

For steps 1-3... you might could try looking at the code from here: http://browsershots.org/. Not sure if it would be relevant - it seems like it requires a lot of setup. Maybe their architecture could work?

A couple of questions and suggestions:

  • Do you really need it converted to PDF? Why? In some cases, it would be better to stick with HTML.
  • Is upgrading the hardware of the server that generates the PDF an option? I asked this because if all the libraries that you've tried is taking too long to create, then your only option might be upgrading the server.
  • You might want to solve the problem with the command line error. If it gives the fastest results, then find a work around it.
xmedeko

Try HTMLDOC commandline tool project https://www.msweet.org/projects.php?Z1

There are many solution to convert HTML to PDF, I can suggest you the one by https://grabz.it.

The have a flexible PHP API which can be used by cronjobs or directly from PHP web page.

If you want to try it, at first you should get an app key + secret for authorization and the development free SDK

Here is an example of a basic implementation.

//First init
include("GrabzItClient.class.php");

// Create the GrabzItClient class
// Replace "APPLICATION KEY", "APPLICATION SECRET" values for your account!
$grabzIt = new GrabzItClient("Application Key", "Application Secret");

// To take a PDF screenshot
$grabzIt->URLToPDF("http://www.google.com");

// To save in case public callback handler is available
$grabzIt->Save("http://www.example.com/handler.php");   
// OR To save in case public callback handler is not available,
// it's a synchonous method can be usedthe will force your application to wait 
// while the screenshot is created
$filepath = "images/result.jpg";
$grabzIt->SaveTo($filepath);    

It's possible to get other kinds of screenshots such as image screenshot and etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!