Php 如何使用html2pdf在ajax调用后转换动态html

Php 如何使用html2pdf在ajax调用后转换动态html,php,jquery,html,ajax,pdf,Php,Jquery,Html,Ajax,Pdf,我正在尝试在html页面中动态设置数据,然后转换为pdf并下载,这是我的呼叫url: createPDF.php?id=78 这是我在createPDF.php中的html部分: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script type="text/javasc

我正在尝试在html页面中动态设置数据,然后转换为pdf并下载,这是我的呼叫url:

createPDF.php?id=78

这是我在createPDF.php中的html部分:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
function func(){
       jQuery.ajax({
        url:'******************/products/78',
        type:'GET',
        success:function(response)
        {

            $.each(response,function(index,product){
                jQuery("#pdfTitle").append( product.title );
                jQuery("#pdfDescription").append(product.description);
                jQuery("#pdfImage").append('<img class="img-responsive" src="' + product.meta_description + '" />');
                console.log('title='+product.title);
            });
        }
    });
}
</script>
</head>
<body onload="func()">  
    <h1 style="color:red;">Description</h1>
    <p id="pdfDescription"></p>
    <p>Copyright © Test</p>
</body>
</html>
但我总是空的pdf,这就是控制台中发生的情况:

createPDF.php?id=78:94资源被解释为文档,但 使用MIME类型应用程序传输/pdf


您需要在文档开头添加
,以便
ob_get_clean()
工作。

您可以使用jspdf jquery插件将html转换为pdf文件。我尝试使用jspdf,但我在样式和颜色方面有问题:(var doc=new jspdf();//我知道正确的拼写是color;)doc.setTextColor(100);文档文本(20,20,‘这是灰色的’);我有我的owen html,这是一个相对的解决方案,我想用她的风格(颜色位置,标题…)转换hoal页面
$content = ob_get_clean();
// convert to PDF
require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');


$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$html2pdf->Output('Oumaya.pdf', 'D');