topdf.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Markdown
  2. // We use GithubMarkdown to keep some of the HTML tags
  3. var Markdown = require('markdown-to-html').GithubMarkdown;
  4. // PDF
  5. const HTML5ToPDF = require("html5-to-pdf");
  6. const path = require("path");
  7. // HTML
  8. const fs = require('fs');
  9. // Catch CLI Parameters
  10. function getArgs () {
  11. // Source: https://stackoverflow.com/a/54098693
  12. const args = {};
  13. process.argv
  14. .slice(2, process.argv.length)
  15. .forEach( arg => {
  16. // long arg
  17. if (arg.slice(0,2) === '--') {
  18. const longArg = arg.split('=');
  19. const longArgFlag = longArg[0].slice(2,longArg[0].length);
  20. const longArgValue = longArg.length > 1 ? longArg[1] : true;
  21. args[longArgFlag] = longArgValue;
  22. }
  23. // flags
  24. else if (arg[0] === '-') {
  25. const flags = arg.slice(1,arg.length).split('');
  26. flags.forEach(flag => {
  27. args[flag] = true;
  28. });
  29. }
  30. });
  31. return args;
  32. }
  33. const args = getArgs();
  34. // Markdown initiation
  35. var md = new Markdown();
  36. md.bufmax = 2048;
  37. var fileName = 'test.md';
  38. var opts = {title: 'File $BASENAME in $DIRNAME', stylesheet: 'style.css'};
  39. var html = '';
  40. // PDF conversion function
  41. const convertToPDF = async (html) => {
  42. const html5ToPDF = new HTML5ToPDF({
  43. inputBody: html,
  44. outputPath: path.join(__dirname, "output.pdf"),
  45. pdf: {
  46. format: 'A4',
  47. margin: {
  48. top: '0.5in',
  49. bottom: '0.6in',
  50. right: '0.5in',
  51. left: '0.5in',
  52. },
  53. displayHeaderFooter:true,
  54. headerTemplate: '<div id="header-template"></div>',
  55. footerTemplate: '<div id="footer-template" style="font-size:10px !important; color:#808080; padding-left:10px; margin:10px auto;">Page <span class="pageNumber"></span></div>',
  56. },
  57. include: [
  58. path.join(__dirname, "style.css"),
  59. ],
  60. });
  61. await html5ToPDF.start();
  62. await html5ToPDF.build();
  63. await html5ToPDF.close();
  64. console.log("PDF: DONE!");
  65. process.exit(0);
  66. }
  67. // source: https://stackoverflow.com/a/35893166
  68. var streamToString = function(stream, callback) {
  69. var str = '';
  70. stream.on('data', function(chunk) {
  71. str += chunk;
  72. });
  73. stream.on('end', function() {
  74. callback(str);
  75. });
  76. }
  77. // Conversion from markdown code (and eventually to PDF)
  78. md.render(fileName, opts, function(err) {
  79. if (err) {
  80. console.error('>>>' + err);
  81. process.exit();
  82. }
  83. // Write HTML and convert to PDF with the html retrieved
  84. streamToString(md, function(html) {
  85. if ( !(args['skip-pdf']) ) {
  86. try {
  87. convertToPDF(html)
  88. } catch (error) {
  89. console.error(error)
  90. }
  91. }
  92. if ( !(args['skip-html']) ) {
  93. fs.writeFile("output.html", html, function(err) {
  94. if (err) {
  95. return console.log(err);
  96. }
  97. console.log("HTML: DONE!");
  98. });
  99. }
  100. });
  101. });