Generate pdf from HTML in div using Javascript

前端 未结 12 1208
你的背包
你的背包 2020-11-22 06:10

I have the following html code:



    
        

don\'t print th

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 06:33

    • No depenencies, pure JS
    • To add CSS or images - do not use relative URLs, use full URLs http://...domain.../path.css or so. It creates separate HTML document and it has no context of main thing.
    • you can also embed images as base64

    This served me for years now:

    export default function printDiv({divId, title}) {
      let mywindow = window.open('', 'PRINT', 'height=650,width=900,top=100,left=150');
    
      mywindow.document.write(`${title}`);
      mywindow.document.write('');
      mywindow.document.write(document.getElementById(divId).innerHTML);
      mywindow.document.write('');
    
      mywindow.document.close(); // necessary for IE >= 10
      mywindow.focus(); // necessary for IE >= 10*/
    
      mywindow.print();
      mywindow.close();
    
      return true;
    }
    

提交回复
热议问题