generate-pdf.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { mdToPdf } = require('md-to-pdf');
  2. const path = require('path');
  3. const files = process.argv.slice(2);
  4. (async () => {
  5. for (const file of files) {
  6. const absPath = path.resolve(file);
  7. console.log(`Génération PDF : ${absPath}`);
  8. try {
  9. const pdf = await mdToPdf(
  10. { path: absPath },
  11. {
  12. launch_options: { args: ['--no-sandbox', '--disable-setuid-sandbox'] },
  13. css: `
  14. body { font-family: Arial, sans-serif; font-size: 13px; line-height: 1.6; margin: 40px; }
  15. h1 { color: #1a1a2e; border-bottom: 2px solid #1a1a2e; padding-bottom: 6px; }
  16. h2 { color: #16213e; margin-top: 24px; }
  17. h3 { color: #0f3460; }
  18. table { border-collapse: collapse; width: 100%; margin: 12px 0; }
  19. th { background: #1a1a2e; color: white; padding: 8px 12px; text-align: left; }
  20. td { padding: 7px 12px; border-bottom: 1px solid #ddd; }
  21. tr:nth-child(even) td { background: #f5f7fa; }
  22. code { background: #f0f0f0; padding: 2px 5px; border-radius: 3px; font-size: 12px; }
  23. pre { background: #f0f0f0; padding: 12px; border-radius: 4px; }
  24. blockquote { border-left: 4px solid #ccc; padding-left: 12px; color: #666; }
  25. `,
  26. pdf_options: {
  27. format: 'A4',
  28. margin: { top: '20mm', bottom: '20mm', left: '20mm', right: '20mm' },
  29. printBackground: true,
  30. },
  31. }
  32. );
  33. if (pdf) {
  34. const outPath = absPath.replace(/\.md$/, '.pdf');
  35. require('fs').writeFileSync(outPath, pdf.content);
  36. console.log(`✅ PDF généré : ${outPath}`);
  37. }
  38. } catch (err) {
  39. console.error(`❌ Erreur pour ${file} :`, err.message);
  40. }
  41. }
  42. })();