jsPDF fromHTML() not show image

风流意气都作罢 提交于 2019-12-25 03:57:24

问题


HTML CODE:

<div id="customers" >
   <div class="">
  <div class="heading">
      <img class="form_head" src="http://dev.syntrio.in/kchr-project/images/kchr-kchr.png" alt="">
</div>
<button onclick="javascript:demoFromHTML();">PDF</button>

JS CODE:

function demoFromHTML() {
    var pdf = new jsPDF('p', 'pt', 'letter');
    // source can be HTML-formatted string, or a reference
    // to an actual DOM element from which the text will be scraped.
    source = $('#customers')[0];

    // we support special element handlers. Register them with jQuery-style 
    // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
    // There is no support for any other type of selectors 
    // (class, of compound) at this time.
    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function (element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 40,
        width: 10000
    };
    // all coords and widths are in jsPDF instance's declared units
    // 'inches' in this case
    pdf.fromHTML(
    source, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {
        // dispose: object with X, Y of the last line add to the PDF 
        //          this allow the insertion of new lines after html
        pdf.save('Test.pdf');
    }, margins);
}

I am included jspdf.debug.js. When I click the pdf button, pdf was generated, but the image in the html code is not displayed. I got a blank pdf. Please help me to fix this problem


回答1:


I had the same problem, but my image is not seen because i tried it in chrome , firefox tried it on and if it worked , indicating that the code is fine. I clarify that I do not know much this topic , but if you want to operate in any browser you can use a web server.




回答2:


Try this,

I am Changed my above html code to :

<div id="customers" >
   <div class="">
  <div class="heading">
      <img class="form_head" src="base64encoded data of the  image" alt="">
</div>
<button onclick="javascript:demoFromHTML();">PDF</button>

Then its works for me

jsFiddle Example



来源:https://stackoverflow.com/questions/33951936/jspdf-fromhtml-not-show-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!