Blame view

html2pdf 1.13 KB
73f2ae9d   Geoffrey PREUD'HOMME   Outils de rédaction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #!/usr/bin/env node
  
  // Imports
  var fs = require('fs');
  var pdf = require('html-pdf');
  var yargs = require('yargs');
  
  
  // Understanding
  var argv = yargs
      .usage("Usage: $0 -o out.pdf [options]")
      .example('$0 -i doc.pdf -o doc.pdf', 'Convert doc.html to PDF using the default values')
      .help('h')
      .alias('h', 'help')
  
      .describe('i', 'Input file')
      .alias('i', 'input')
      .default('i', '/dev/stdin')
  
      .describe('o', 'Output file')
      .alias('o', 'output')
  
      .describe('t', 'Title of file')
      .alias('t', 'title')
      .default('t', 'Sans titre')
  
      .demandOption(['o'])
      .argv;
  
  
  // Settings
  options = {
    "base": "file://" + process.cwd() + '/',
    "format": "A4",
    "orientation": "portrait",
    "border": "2cm",
  
    "footer": {
      "height": "10mm",
      "contents": {
          default: '<div style="text-align: left; float: left;">' + argv.title + '</div> <div style="text-align:right; float: right;">{{page}}/{{pages}}</div>',
      }
    },
  }
  
  // Reading
  htmlString = fs.readFileSync(argv.i, "utf8");
  
  // Conversion
  pdf.create(htmlString, options).toFile(argv.o, function(err, res) {
      if (err) console.error(err);
  });