如何在模态中打开jsPDF

时间:2017-06-08 20:31:10

标签: javascript jspdf

我有这段代码使用jsPDF库生成我的PDF文件:

// JSPDF, docs: http://rawgit.com/MrRio/jsPDF/master/docs/global.html   

                                            // Default export is a4 paper, portrait, using milimeters for units
                                            var doc = new jsPDF()

                                            // set font size
                                            doc.setFontSize(12);


                                            doc.text("Invoice #" + row["client_id"],10,10);
                                            doc.text("Name: " + row["name"],10,14);
                                            doc.text("Gender: " + row["gender"], 10, 18);
                                            doc.text("Company: " + row["company"], 10, 22);

                                            //doc.save("a4.pdf");   

                                            doc.autoPrint();
                                            doc.output("dataurlnewwindow");

¿有可能在模态窗口中打开吗?

使用此脚本:

$.ajax({
type : "POST",
url : getPrintablePDF,
dataType : "json",
contentType : 'application/json; charset=utf-8',
data : JSON.stringify(params),
success : function(data) {
    var myResponse = eval(data);
    $("<iframe />") // create an iframe
      // add the source
      .attr('src', 'data:application/pdf;base64,' + myResponse.base64EncodedResponse)
      .appendTo('.modal-body'); // append to modal body or wherever you want
}}); 

谢谢!

2 个答案:

答案 0 :(得分:0)

我有类似的要求。由于模态不能直接解码jdpdf的dataUristring,我在一个iframe中加载了jspdf,然后在一个模态中打开了iframe。

$('.modal').on('shown.bs.modal',function(){
        $(this).find('iframe').html("").attr("src", pdf.output('datauristring'));
    })

答案 1 :(得分:0)

    import org.unbescape.*
    import org.unbescape.html.*

    class UnbescapeTagLib {

        static defaultEncodeAs = [taglib:'text']
        //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
        static final namespace = 'ubs'

        def unescape = {attrs, body ->
            out << HtmlEscape.unescapeHtml(body.call().toString())
        }
    }