【问题标题】:How to use PHP to generate PDF from HTML which gets duplicated on both halves of the PDF (landscape version)如何使用 PHP 从 HTML 生成 PDF,在 PDF 的两半(横向版本)上重复
【发布时间】:2016-08-11 20:42:38
【问题描述】:

我正在用 PHP 编写并使用 DOMPDF、FPDF 和 TCPDF 来呈现 PDF 文档。

有谁知道将 HTML 格式的 PDF 渲染到在横向 PDF 页面的两半上重复 html 的脚本是什么?我会附上一张图片来准确表达我的意思。

诀窍是绝对没有边距,它是字母大小,最后是横向格式。

这里有一些可能有效的代码,但我想我要求更多的 HTML 方面的东西。

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('myFile.html');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Letter', 'landscape');

// Render the HTML as PDF
$dompdf->render();

【问题讨论】:

  • StackOverflow 不是那种给我代码的女孩。 ;)
  • @JayBlanchard 我意识到这一点,但我终于解决了这个问题。我的答案已发布。顺便说一句,我从我之前提出的一个问题中记得你。谢谢你一直在我身边,即使是现在大声笑
  • @JayBlanchard 我需要帮助解决一个单独的问题。希望你能帮助我stackoverflow.com/questions/36752092/…

标签: php html tcpdf fpdf dompdf


【解决方案1】:

编辑:您需要此资源:https://github.com/dompdf/dompdf

我的 PHP DOMPDF 代码:

$fp = fopen('file_html.html','w');

require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');

function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
    ini_set("memory_limit", "50M");
    $dompdf = new DOMPDF();
    $dompdf->set_paper($paper,$orientation);
    $dompdf->load_html($html);
    $dompdf->render();
    $pdf = $dompdf->output();
    @file_put_contents($filename . ".pdf", $pdf);
}

$filename = 'myPDFFileName';
$dompdf = new DOMPDF();
$html = file_get_contents('file_html.html'); 
pdf_create($html,$filename,'Letter','landscape');

这会从“file_html.html”中提取所有 HTML 并将其呈现为名为“myPDFFileName.pdf”的 PDF 到与主 PHP 文件相同的文件夹中。

这是我的html:

<html>
<head>
    <style>
        @page { margin: 0px 0px 0px 0px; }
        body {
            height:100%;
            margin: 0px 0px 0px 0px;
            background-size: 100% 100%;
            background-repeat: no-repeat;
        }
        div.mainLayout{float:left;width:50%}
    </style>
</head>
<body>
    <div class="mainLayout" style="height:100%">
        <p>Context</p>
    </div>
    <div class="mainLayout" style="height:100%">
        <p>Context</p>
    </div>
</body>
</html>

得到了我想要的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2013-03-07
    • 1970-01-01
    • 2011-12-31
    • 2019-01-02
    • 1970-01-01
    • 2016-03-23
    • 2013-07-04
    相关资源
    最近更新 更多