create pdf from image using jspdf, pdf make js and html2canvas - Javascript Canvas

Javascript examples for Canvas:image

Description

create pdf from image using jspdf, pdf make js and html2canvas

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.js"></script> 
      <script type="text/javascript" src="https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js"></script> 
      <style id="compiled-css" type="text/css">

#content {/* ww w  . ja  v a 2s . com*/
   position: absolute;
   width: 300px;
   height: 200px;
   border: 5px solid red;
   overflow: hidden;
}
#img1 {
   width: 300px;
   height: 200px;
   position: absolute;
   z-index: 5;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
function renderContent() {
    html2canvas(document.getElementById("content"), {
        allowTaint: true
    }).then(function(canvas) {
        document.getElementById("result").appendChild(canvas);
    });
}
var canvas3 = window._canvas3 = new fabric.Canvas('c3');
function renderContentPdf() {
   fabric.Image.fromURL('https://www.java2s.com/style/demo/Google-Chrome.png', function (img) {
    canvas3.add(img.set({
        left: 50,
        top: 50,
        angle: 30
    }));
   // save to pdf
    var docDefinition = {
      content: [{
        image: canvas3.toDataURL(),
        width:400
      }]
    };
    pdfMake.createPdf(docDefinition).download("divwrap.pdf");
}, {
    crossOrigin: 'Anonymous'
});
}
document.getElementById("button").onclick = renderContent;
document.getElementById("buttonPdf").onclick = renderContentPdf;
    }

      </script> 
   </head> 
   <body> 
      <script src="https://rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.min.js"></script> 
      <div id="content"> 
         <img id="img1" src="https://www.java2s.com/style/demo/Google-Chrome.png"> 
      </div> 
      <canvas id="c3" width="400" height="400"></canvas> 
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br> 
      <input id="button" type="button" value="convert div to image">
      <br> 
      <input id="buttonPdf" type="button" value="convert div to pdf">
      <br> 
      <h3>result:</h3> 
      <div id="result"></div>  
   </body>
</html>

Related Tutorials