使用mpdf通过AJAX生成PDF [英] Generating PDF via AJAX using mpdf

查看:243
本文介绍了使用mpdf通过AJAX生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mpdf库生成用户生成的HTML的PDF。我可以将PDF成功保存到服务器,但我希望在浏览器中为用户打开PDF。我尝试使用mpdf的输出选项在浏览器中打开文件或提示下载,但是当我使用AJAX将html数据发送到脚本时都没有发生。

I'm using the mpdf library to generate a PDF of user-generated html. I can get the PDF to save to the server successfully, but I want the PDF to open in the browser for the user. I've tried using mpdf's output options to open the file in the browser or to prompt a download, but neither happens when I use AJAX to send the html data to the script.

这是我的AJAX:

$('#save').click(function() {

        var shelf_clone = $('#shelf').clone();
        var shelf = shelf_clone.prop('outerHTML'); 

        $.ajax({
            type: "POST",
            url: "pdf.php",
            data: { html:shelf },
            success: function(response)
            {
                $('#status').html('File Saved Successfully');
            },
        })

    });

这是我的PDF生成脚本:

Here's my PDF-generating script:

<?php

include_once('/mpdf/mpdf.php');

$html = $_POST['html'];

$mpdf=new mPDF();
$stylesheet = file_get_contents('css/print.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output('shelf.pdf', I);

exit;

?>

我正在使用AJAX,因此无需离开页面即可创建PDF。我的代码中是否有错误,或者我应该使用不同的方法?

I'm using AJAX so that the PDF can be created without having to navigate away from the page. Is there an error in my code or should I be using a different approach?

推荐答案

我最终没有使用AJAX,而是在表单上添加了一个隐藏的输入,并使用以下脚本填充它:

I ended up not using AJAX and instead added a hidden input on the form and populated it using the following script:

$('#save').click(function() {

    event.preventDefault();

    var shelf_clone = $('#shelf').clone();
    var shelf = shelf_clone.prop('outerHTML');

    $('#save_shelf input[name=shelf]').val(shelf);

    $('#save_shelf').submit();

});

这篇关于使用mpdf通过AJAX生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆