HTML2PDF页面大小

Lar*_*rry 7 php html2pdf

我正在使用PHP和HTML2PDF lib生成pdf文件.但我要做的是生成一个pdf文件,其pageSize(宽度/高度)为html内容大小.我怎样才能做到这一点?

我的HTML内容是:

<page format="432x240" orientation="L" backcolor="#FFFFFF" style="font: arial;">
<div class="image">
    <span class="firstname">$fname</span>
    <span class="lastname">$lname</span>
</div>
Run Code Online (Sandbox Code Playgroud)

图像类的CSS是:

position: relative;width: 100%; /* for IE 6 */ background-image: url(../img/test.png);height: 240px; width: 432px;top: 50%;
Run Code Online (Sandbox Code Playgroud)

我的PHP代码是:

$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', 0);
$html2pdf->pdf->SetDisplayMode('fullpage');

$contentTpl = $this->renderPartial('template_01', array('fname' => $firstname, 'lname' => $lastname), true);
            $html2pdf->writeHTML(utf8_encode($contentTpl));
Run Code Online (Sandbox Code Playgroud)

小智 8

以下是此问题的解决方案:

$html2pdf = new HTML2PDF('P', array($width_in_mm,$height_in_mm), 'en', true, 'UTF-8', array(0, 0, 0, 0));
Run Code Online (Sandbox Code Playgroud)

宽度和高度应为MM.如果您使用英寸将其转换为MM.

式:

$width_in_mm = $width_in_inches * 25.4; 
$height_in_mm = $height_in_inches * 25.4;
Run Code Online (Sandbox Code Playgroud)

不要四舍五入.即使它有一个小数点,也使用精确的转换.

希望这个答案能解决你的问题.