【问题标题】:convert HTML having text and image to pdf and create Image of pdf using PHP or javascript将具有文本和图像的 HTML 转换为 pdf 并使用 PHP 或 javascript 创建 pdf 的图像
【发布时间】:2016-05-02 17:02:39
【问题描述】:

我想使用 PHP 或 javascript 将 html 转换为 PDF。 我有一个包含文本和图像的 html。 我曾尝试使用 html2PDF,但在图像中遇到问题。 如果我使用相对路径,则不允许创建 PDF 并抛出关于未找到图像的错误,并且使用绝对路径它不会在我的 Web 视图中显示图像。 所以有人有解决方案吗? 有没有其他解决方案将 html 转换为 PDF 或图像?

需要在 PDF 中创建超过 1 个页面并创建所有这些页面图像

【问题讨论】:

  • 寻找HTML 2 PDF它的免费stackoverflow.com/questions/391005/…
  • 使用FPDF,我用过一次fpdf.org
  • 更喜欢使用 DOMPDF - link。看看它的文档。它非常易于使用,并且图像在这方面工作得非常好。
  • 我使用 TCPDF 从 HTML 创建 PDF .. 我有 html 内容数组 .. 我想在 pdf 的一页中放一个完整的 html .. 如果我将自动分页设置为 true 然后如果它会为 html 创建多个页面,那么在它的限制内容不显示之后,这怎么可能呢??

标签: javascript php jquery html html5-canvas


【解决方案1】:

`您必须使用表格内容和打开 PDF 的链接创建 HTML 或 PHP 文件

<table width="500px" cellpadding="5px" cellspacing="5px" border="1">
    <tr bgcolor="#CCCCCC">
        <td>Nombre</td>
        <td>Apellido</td>
        <td>Email</td>
        <td>Edad</td>
    </tr>
    <tr bgcolor="#FF9933">
        <td>Antonio</td>
        <td>López</td>
        <td>alopez@gmail.com</td>
        <td>25</td>
    </tr>
    <tr bgcolor="#FF9933">
        <td>Sergio</td>
        <td>Martínez</td>
        <td>sgm@gmail.com</td>
        <td>47</td>
    </tr>
    <tr bgcolor="#FF9933">
        <td>Natalia</td>
        <td>Estrada</td>
        <td>natty@gmail.com</td>
        <td>22</td>
    </tr>
</table>
<p><a href="pdf.php">Ver tabla en PDF</a></p>




**Now only missing the key issue , the content of pdf.php file :**



<?php ob_start(); ?>
<h2>Lista de usuarios</h2>
<table width="500px" cellpadding="5px" cellspacing="5px" border="1">
    <tr bgcolor="#CCCCCC">
        <td>Nombre</td>
        <td>Apellido</td>
        <td>Email</td>
        <td>Edad</td>
    </tr>
    <tr bgcolor="#FF9933">
        <td>Antonio</td>
        <td>López</td>
        <td>alopez@gmail.com</td>
        <td>25</td>
    </tr>
    <tr bgcolor="#FF9933">
        <td>Sergio</td>
        <td>Martínez</td>
        <td>sgm@gmail.com</td>
        <td>47</td>
    </tr>
    <tr bgcolor="#FF9933">
        <td>Natalia</td>
        <td>Estrada</td>
        <td>natty@gmail.com</td>
        <td>22</td>
    </tr>
</table>
<?php
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$pdf = $dompdf->output();
$filename = "ejemplo".time().'.pdf';
file_put_contents($filename, $pdf);
$dompdf->stream($filename);
?>

希望对你有帮助

。 `

【讨论】:

    【解决方案2】:

    有几个使用 php 的 html 程序总是有小错误,如果你将 pdf 转换为 html 并且 php 可以证明“wkhtmltopdf”或“DOMPDF”aunq 可以给你错误表 Which one is the best PDF-API for PHP? 这是最好的转换器

    【讨论】:

    • 我使用过 TCPDF,但它在显示图像方面存在一些问题。因为我已经在它的库中进行了相同的更改..并且工作正常..但是我有多个页面和它应该像每个页面都显示在 pdf 中的单页中,例如……如果我有 5 页,那么 pdf 有 5 页,并且每个页面都应该包含与 HTML 相同的内容。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多