我很困惑。
我尝试用MPDF导出一个html页面。我看到了一些例子,如何使用它是这样的:
<?php
$html = '
<h1><a name="top"></a>mPDF</h1>
<p>P: Nulla felis erat, imperdiet eu, ullamcorper non, nonummy quis, elit. Suspendisse potenti. Ut a eros at ligula vehicula pretium. Maecenas feugiat pede vel risus. Nulla et lectus. </p>';
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
因此,我必须将html代码放入一个变量中,然后mpdf类将生成pdf文件。
我有这样的问题:
我想打印一个页面,该页面在用户提交表单后使用php生成。这是一个报告页面。用户选择他们想要的特定时间段,页面将显示报告。
因此,我必须在用户提交表单后获得生成的html代码。我使用ob_get_contents()
来完成此操作,然后将其与表单一起发送到上面有mpdf脚本的php页面,比如pdf.php.
这是我在pdf.php上的代码:
<?php
include("../mpdf.php");
$mpdf=new mPDF();
$htmlx = "$_POST[htmlx]";
$mpdf->WriteHTML($htmlx);
$mpdf->Output();
exit;
?>
pdf已经创建,但它没有正确显示(创建的pdf没有正确显示html页面)。
我认为可能生成的html代码是错误的,mpdf不理解html代码。所以我试着这样做:
我已经尝试复制生成的html代码,并使php页面像上面的示例一样。生成的pdf文件正确显示在页面上!
我不明白为什么会发生这种事。也许我不能通过php中的表单发送html代码?
我很抱歉我的英语不好。我真的希望你能理解我的问题,并能解决帮助我解决它。非常感谢。
编辑:
在这里我的表格发送html代码:
<?php
$html = ob_get_contents();
$html = str_replace('"',''"',$html);
?>
<form action="pdf.php" method="POST" enctype="multipart/form-data">
<input type="submit" value="Print to PDF">
<textarea name="htmlx" style="display:; width: 100%; height:300px;" readonly><?= $html ?></textarea>
</form>
编辑
这是我生成的html:的示例
<style>
*{
font-family: arial;
}
body{
background: #fff;
}
table#myTable{
border:solid 7px #eee;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
-khtml-border-radius: 7px;
border-radius: 7px;
behavior: url(/files/border-radius.htc);
}
#myTable th{
font: normal 11pt 'century gothic';
cursor:pointer;
background: #ccc;
padding: 8px 15px 8px 15px;
color:#000
}
#myTable th.header {
padding: 8px 15px 8px 15px;
background: #eee;
cursor: pointer;
font-weight: bold;
background-repeat: no-repeat;
background-position: center right;
padding-right: 20px;
margin-left: -1px;
color:#fff
}
#myTable th.headerSortUp {
background-image: url(/template/default/images/asc.gif);
background-color: #eee;
color:#fff
}
#myTable th.headerSortDown {
background-image: url(/template/default/images/desc.gif);
background-color: #eee;
color:#fff
}
#myTable tr {
border:solid 1px #3e6189;
}
#myTable td {
background:#f9f9f9;
padding: 8px 15px 8px 15px;
border:solid 1px #f0f0f0;
font-size: 9pt
}
@media print
{
.blabla{
display:none;
}
}
</style>
</head>
<body>
<script>
function printPage() { print(); }
</script>
<table cellpadding="3" cellspacing="1" border="0" width="100%" id="myTable">
<tr class="bgTransUng25">
<th width="5">No</th>
<th width="70">Pembelian</th>
<th width="100">Oleh</th>
<th width="70">Tanggal</th>
<th width="">Detail Pembelian</th>
<th width="70">Paket Pengiriman</th>
</tr>
<tr class="bgTransWht71" align="center">
<td align="right">1.</td>
<td>00122</td>
<td>Iko Uwais</td>
<td align="right">2 Jul 2012</td>
<td>
<table cellpadding="3" cellspacing="1" border="0" width="100%">
<tr class="bgTransWht25" align="center">
<td align="left">Kaos BW 9500 003</td>
<td align="right" width="70">3 pcs</td>
<td width="100"><div style="text-align:left">Rp.<div style="float:right; text-align:right">285.000</div></div></td>
</tr>
<tr class="bgTransWht25" align="center">
<td align="left">Kaos BW 9500 003</td>
<td align="right" width="70">7 pcs</td>
<td width="100"><div style="text-align:left">Rp.<div style="float:right; text-align:right">665.000</div></div></td>
</tr>
<tr class="bgTransWht25" align="center">
//and so on...
更换
$html = str_replace('"',''"',$html);
带有:
$html = htmlentities($html);
HTML不使用反斜杠进行转义,而是使用实体代码。
我认为问题是…
"display:;"
把它全部去掉。显示应该默认为至少显示文本区域的内容。
你应该试试stripslashes()
,对我有用:
$mpdf->WriteHTML( stripslashes($htmlx) );