2015-11-27 58 views
0

HTML代碼:jsPDF fromHTML()不顯示圖像

<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代碼:

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); 
} 

我包括jspdf.debug.js。當我點擊PDF按鈕時,生成了pdf,但不顯示HTML代碼中的圖像。我有一個空白的pdf。請幫我解決這個問題

回答

0

我有同樣的問題,但沒有看到我的圖像,因爲我在鉻試用它,Firefox試圖它,如果它的工作,表明代碼是好的。我澄清說我不太瞭解這個主題,但是如果你想在任何瀏覽器中運行,你可以使用一個web服務器。

+0

我得到了一個解決方案。但我不知道它的正確方法。我將圖像轉換爲base64編碼,然後運行 –

+0

你能告訴我嗎?,我將不勝感激。 –

-1

試試這個,

我改變了我上面的html代碼:

<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> 

那麼它的作品對我來說

jsFiddle Example