如何使用PHP从HTML生成PDF,这些PDF在PDF的两半上都是重复的(横向版本)

时间:2016-04-19 15:04:56

标签: php html tcpdf fpdf dompdf

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

有没有人确切地知道脚本将HTML格式的PDF呈现在横向格式化PDF页面的两半重复html的位置?我将包含一张图片,以准确显示我的意思。

诀窍是绝对没有边距,最后是Letter尺寸,横向格式化。

HTML Rendered two halves of page PDF

这里有一些可能有效的代码,但我想我还要求更多的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();

1 个答案:

答案 0 :(得分:0)

编辑:您需要此资源: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。并将其呈现为PDF命名为" myPDFFileName.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>

得到我正在寻找的结果。

相关问题