使用HTML和PHP生成MPDF到PDF

时间:2018-11-03 11:05:16

标签: php html mpdf

这是我要在其上创建MPDF的CV的代码,我在另一页上有MPDF代码。内容显示在pdf上,但是当我想要实际的php值时,却带有php echo值

<?php
            //Image upload
            $uploaddir = "images\\";
            $uploadfile = $uploaddir . $userTime . basename($_FILES['myimg']['name']); 
            move_uploaded_file($_FILES["myimg"]["tmp_name"], "$uploadfile");
            $_SESSION['myimg'] = $uploadfile;

            if(isset($_FILES['myimg'])){
                $file = $_FILES['myimg']['name'];
                //echo "" .$file;
                //echo "<img src='" . $file . "'>";
                echo '<img src="' . $uploadfile . '" width="40%" height="40% "/>'; 
            }
        ?>
        <p>First Name<br><?php echo $fullName;?></p> 
        <p>Email Address<br><?php echo $emailAddress;?></p>
        <p>Phone Number<br><?php echo $phoneNumber;?></p>`enter code here`
        <p>College<br><?php echo $theEducation;?></p>
        <p>Course<br><?php echo $theCourse;?></p>

<?php

require_once __DIR__ . '/vendor/autoload.php';

    $mpdf = new \Mpdf\Mpdf();

    // $mpdf->WriteHTML(file_get_contents('build.php'));

    $mpdf->Output();
    //$mpdf->Output('filename.pdf','F');

?>

1 个答案:

答案 0 :(得分:0)

将HTML内容构建为字符串:

$html = '';
...
$html .= '<img src="' . $uploadfile . '" width="40%" height="40% "/>'
$html .= '<p>First Name<br>' . $fullName . '</p> 
<p>Email Address<br>' . $emailAddress . '</p>
<p>Phone Number<br>' . $phoneNumber . '</p>
<p>College<br>' . $theEducation . '</p>
<p>Course<br>' . $theCourse . '</p>';

或使用output buffering捕获html。

然后,将HTML传递给mPDF:

$mpdf->WriteHTML($html);