如何将动态PHP文件转换为PDF? [英] how to convert dynamic php file to pdf?

查看:94
本文介绍了如何将动态PHP文件转换为PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将动态php数据库文件转换为pdf。
我尝试使用DOMPDF,但是我在定义字符串时遇到了问题。我将解释:

以下是dompdf的'hello world'脚本:

  require_once(安培; QUOT; dompdf_config.inc.php&安培; QUOT); 
$ html =
'< html>< body>'。
'< p> Hello World!< / p>'。
'< / body>< / html>';
$ dompdf = new DOMPDF();
$ dompdf-> load_html($ html);
$ dompdf-> render();
$ dompdf-> stream(hello_world.pdf);

事情是这样的,而不是简单的

  '< HTML><身体GT;'。 
'< p> Hello World!< / p>'。
'< / body>< / html>'

文件充满了函数和sql查询。因为它里面有很多,,等符号,我在这个页面也有一个javascript动态图表(jqplot)。 所以不是pdf文件我收到错误错误错误...



有没有人有解决方案?



任何答案,并将非常感谢您的解决方案...

解决方案

首先,您需要从您的PHP,然后将它传递给DOMPDF:

 <?php 
require_once(dompdf_config.inc.php) ;
ob_start();
require_once(path / to / input / file.php);
$ dompdf = new DOMPDF();
$ dompdf-> load_html ();
$> $ gt;
$& code>

您也可以执行常规HTTP请求:

 <?php 
require_once(dompdf_config.inc.php);
$ dompdf = new DOMPDF();
$ dompdf-> load_html_file( http://example.com/file.php);
$ dompdf-> render();
$ dompdf-> stream(file.pdf);
?>






如果您需要JavaScript支持,请尝试 wkhtmltopdf ,它基于Webkit并且完美无缺。


I try to convert dynamic php database file to pdf. I try with DOMPDF, but I have a problem with defining the string. I'll explain:

Here is a 'hello world' script for dompdf:

require_once(&quot;dompdf_config.inc.php&quot;);
$html =
    '<html><body>'.
    '<p>Hello World!</p>'.
    '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello_world.pdf");

The thing is that instead of a simple

'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>' 

I have a long php file full of functions and sql queries. because of it I have inside it many ",',; e.t.c signs. I also have a javascript dynamic chart (jqplot) in this page.

So instead of pdf file I get errors errors errors...

Does anyone has a solution for this?

I will really appreciate any answer, and will be really really thankful for a solution...

解决方案

First you'll need to generate HTML from your PHP, then pass it to DOMPDF:

<?php
    require_once("dompdf_config.inc.php");
    ob_start();
    require_once("path/to/input/file.php");
    $dompdf = new DOMPDF();
    $dompdf->load_html(ob_get_clean());
    $dompdf->render();
    $dompdf->stream("file.pdf");
?>

You can also do a regular HTTP request:

<?php
    require_once("dompdf_config.inc.php");
    $dompdf = new DOMPDF();
    $dompdf->load_html_file('http://example.com/file.php');        
    $dompdf->render();
    $dompdf->stream("file.pdf");
?>    


If you need JavaScript support, try wkhtmltopdf, it's based on Webkit and does it's work perfectly.

这篇关于如何将动态PHP文件转换为PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆