将动态HTML转换为PDF使用html2pdf

3

我要转换的页面是通过PHP动态从MySQL数据库生成的。换句话说,页面上的信息取决于所选择对象的Id。因此,我通常会有这样一个URL:

renal_prescRequest_review.php?id=4

我正在使用 html2pdf,但到目前为止我无法完成转换。当我像这样直接传递 id=2

// get the HTML
ob_start();
include("renal_clinicalTrial_review.php?id=2");
$content = ob_get_clean();`

我遇到了以下的错误:
警告:include(renal_prescRequest_review.php?id=2) [function.include]:在/home/renalmed/public_html/testing/test/renal_prescRequest_pdf.php的第5行无法打开流:没有那个文件或目录
警告:include(renal_prescRequest_review.php?id=2) [function.include]:在/home/renalmed/public_html/testing/test/renal_prescRequest_pdf.php的第5行无法打开流:没有那个文件或目录
警告:include() [function.include]:在/home/renalme/public_html/testing/test/renal_prescRequest_pdf.php的第5行中,包含'renal_prescRequest_review.php?id=2'失败(include_path='.:/usr/lib/php:/usr/local/lib/php')
有趣的是这些错误出现在PDF文档上,实际HTML应该被显示。我已经钻研了一段时间了,请帮帮我。
5个回答

2

您不能在包含文件中包含查询字符串。

解决此问题的一种方法是将 renal_prescRequest_review.php 的代码转移到您想要将 HTML 转换为 PDF 的文件中。

以下是示例代码。

<?php
/**
 * HTML2PDF Librairy - example
 *
 * HTML => PDF convertor
 * distributed under the LGPL License
 *
 * @author      Laurent MINGUET <webmaster@html2pdf.fr>
 *
 * isset($_GET['vuehtml']) is not mandatory
 * it allow to display the result in the HTML format
 */
// get the HTML
ob_start();
// database connection here
require_once ( 'database-connection.php' );
// get the id
$id = $_GET['id'];
// Retrieve record from database
// query code here
?>
<page backleft="0mm" backtop="0mm" backright="0mm" backbottom="0mm">    
    ID: <?php echo $id;?>
    <!--other html and php codes here.-->
</page>
<?php
$content = ob_get_clean();
// convert to PDF
require_once('../html2pdf/html2pdf.class.php');
try
{
    $html2pdf = new HTML2PDF('P', array(200,300), 'en', true, 'UTF-8', array(0, 0, 0, 0));
    //$html2pdf = new HTML2PDF('P', 'A4', 'fr', true, 'UTF-8', 0);
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->writeHTML($content);
    //$html2pdf->Output('test.pdf','D'); // force download pdf
    $html2pdf->Output('test.pdf'); // display only
}
catch(HTML2PDF_exception $e) {
    echo $e;
    exit;
}

如果我理解正确的话,您想让我将转换为PDF的代码放在与我的HTML代码相同的文件中。如果这是您建议的,请说明如何处理。如果有可能,请提供一些代码。谢谢。 - Damilare Binutu
在上述代码中,页面标签应该包含HTML,但是此页面生成的HTML不是静态HTML。页面标签将需要由数据库数据填充。因此,它们需要位于<?php ?> 中,并且将必须被回显到页面。 - Damilare Binutu
是的,您可以替换<page>标签内部的HTML代码或PHP代码。在<page>标签内展示您想要的任何内容。我已更新上面的代码,请检查并告诉我是否解决了您的问题。如果是,请标记为答案。谢谢。 - user3045072
谢谢,这个方法非常有效。唯一的注意点是强制下载有些突兀。您能否建议一种处理方式,也许可以使用事件处理程序或链接来解决这个问题? - Damilare Binutu
已更新代码,请查看上方并将其标记为答案并标记此答案为有用。谢谢。 - user3045072
显示剩余2条评论

1
我不确定您是否可以在include中使用get变量。请使用带有file_get_contents函数的url。或者,在include之前删除get变量id并将其分配,通过包含的脚本内isset检测它。

你能否详细介绍一下这个方法,最好附上一个例子。我以前从未使用过html2pdf,而且示例也没有提供我需要的信息。 - Damilare Binutu
不是PDF库出了问题,而是PHP代码。在include中的文件名中删除id=2。 - user1367101
我该如何将PDF文件上传到Amazon S3存储桶? - Shashank Shah
@ShashankShah 这是一个独立的问题。请开始一个新的问题。或者更好的方法是在谷歌上搜索此问题,因为它已经被问过很多次了。 - user1367101
@user1367101 兄弟,最好回答你被问到的问题。我已经发布了一个问题 https://stackoverflow.com/questions/55293465/html2pdf-upload-pdf-file-dierctly-to-the-amazon-s3-bucket 如果你知道这个问题之前已经被问过了,那么请发回答链接,而不是浪费评论。 - Shashank Shah

0

你不能像这样包含文件:

include("renal_clinicalTrial_review.php?id=2");

你应该这样做:

include("renal_clinicalTrial_review.php");

然后将参数传递给调用页面。

例如,如果您的代码保存为test.php,则可以这样调用:

test.php?id=2

0
Warning: include(renal_prescRequest_review.php?id=2)

你不能这样包含文件。在include语句中,你指定了一个查询字符串,而PHP只是期望一个路径。因此,PHP期望找到一个名为renal_prescRequest_review.php?id=2的文件(换句话说,它不是在寻找renal_prescRequest_review.php)。你的PHP应该像这样:

include('renal_prescRequest_review.php');

这样做并不能给OP想要的结果。 - jadkik94
当我这样做时,PDF页面没有显示。但是出现了以下法语错误信息:ERREUR n°3 Fichier : /home/renalmed/public_html/testing/test/html2pdf/_class/parsingHtml.class.php Ligne : 117HTML代码无效,有一个标签关闭过多:<div>HTML:...lank">另存为PDF</a> </form>... - Damilare Binutu
嗯,听起来它试图读取URL。不过,如果没有看到代码本身,我就无法说出它为什么这样做。 - Machavity

0

你可以在此使用会话(session),并在包含文件中删除查询字符串。

session_start();
// get the HTML
ob_start();
$_SESSION['id'] = 2;
include("renal_clinicalTrial_review.php");
$content = ob_get_clean();

在renal_clinicalTrial_review.php文件中,加入session_start()函数,这样你就可以使用session了。

它不起作用,我收到一个法语错误:ERREUR n°3 文件:/home/renalmed/public_html/testing/test/html2pdf/_class/parsingHtml.class.php 行:117HTML代码无效,有一个多余的关闭标签:<div>HTML:...lank">另存为PDF</a> </form>... - Damilare Binutu
请检查 renal_clinicalTrial_review.php 文件,也许你的代码有误。提供的代码应该是可行的,因为我已经在我的一些项目中测试并应用了它。 - user3138249

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接