2017-06-05 25 views
1

我有關於如何創建將下載一個div的PDF文件的按鈕的問題。如何使用javascript創建下載文件並將其保存在服務器中

我試圖window.print()這樣的:

$(document).ready(function(){ 

     $('#printBtn').on('click',function(){ 
      window.print(); 
     }); 

    }); 

,並保存爲PDF文件。我不知道除此之外的其他方式,我不想先保存在服務器上。

+0

檢查HTTPS: //stackoverflow.com/questions/2255291/print-the-contents-of-a-div –

+0

你可以有服務器創建並交付它_而不保存在那裏。所以即時生成。 – arkascha

+0

@AhmedGinani我會檢查它,謝謝你 – rinaldy31

回答

0

您嘗試直接下載使用一個元素屬性下載

<a href="/images/myw3schoolsimage.jpg" download> 

---- ---- OR

<button id="print" onclick="print('uname');" >Print</button> 
<div id="uname"> 
<label>Name</label> 
<label>Spider</label> 
</div> 

<button id="print" onclick="printContent('uname');" >Print</button> 

    function print(e){var divToPrint=document.getElementById('e'); 

    var newWin=window.open('','Print-Window'); 

    newWin.document.open(); 

    newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>'); 

    newWin.document.close(); 

    setTimeout(function(){ 
     newWin.close(); 
    },10); 
} 

Demo Here

+0

用於打印嗎?我需要將文件保存爲pdf ..是否有可能? – rinaldy31

相關問題