Project

General

Profile

1
import * as jsPDF from 'jspdf';
2
import domtoimage from 'dom-to-image';
3

    
4

    
5
export function printPage(sectionID: string) {
6
  const node = document.getElementById(sectionID);
7

    
8
  let img;
9
  let filename;
10
  let newImage;
11

    
12

    
13
  domtoimage.toPng(node, {bgcolor: '#fff'}).then(function (dataUrl) {
14

    
15
    img = new Image();
16
    img.src = dataUrl;
17
    newImage = img.src;
18

    
19
    img.onload = function () {
20

    
21
      const pdfWidth = img.width;
22
      const pdfHeight = img.height;
23

    
24
      // FileSaver.saveAs(dataUrl, 'my-pdfimage.png'); // Save as Image
25

    
26
      let doc;
27

    
28
      if (pdfWidth > pdfHeight) {
29
        doc = new jsPDF('l', 'px', [pdfWidth, pdfHeight]);
30
      } else {
31
        doc = new jsPDF('p', 'px', [pdfWidth, pdfHeight]);
32
      }
33

    
34
      const width = doc.internal.pageSize.getWidth();
35
      const height = doc.internal.pageSize.getHeight();
36

    
37
      doc.addImage(newImage, 'PNG', 10, 10, width, height);
38
      filename = 'mypdf_' + '.pdf';
39
      doc.save(filename);
40

    
41
    };
42

    
43
  }).catch(function(error) {
44
    // Error Handling
45
  });
46
}
(2-2/4)