11#!/usr/bin/env node
22
3- const fs = require ( "fs" ) ;
3+ const fs = require ( "fs/promises " ) ;
44const path = require ( "path" ) ;
55const yargs = require ( "yargs/yargs" ) ;
66const { hideBin } = require ( "yargs/helpers" ) ;
7- const markdownpdf = require ( "markdown -pdf" ) ;
7+ const { mdToPdf } = require ( "md-to -pdf" ) ;
88
99const argv = yargs ( hideBin ( process . argv ) )
1010 . option ( "paper" , {
@@ -31,25 +31,44 @@ const chapters = [
3131 "Cloud.md" ,
3232] . map ( ( f ) => path . resolve ( f ) ) ;
3333
34- // Verify that every chapter exists before starting
35- for ( const file of chapters ) {
36- if ( ! fs . existsSync ( file ) ) {
37- console . error ( `✗ Missing chapter: ${ file } ` ) ;
38- process . exit ( 1 ) ;
34+ ( async ( ) => {
35+ // Ensure all chapters exist
36+ for ( const file of chapters ) {
37+ try {
38+ await fs . access ( file ) ;
39+ } catch {
40+ console . error ( `✗ Missing chapter: ${ file } ` ) ;
41+ process . exit ( 1 ) ;
42+ }
3943 }
40- }
4144
42- const outputPath = path . resolve ( "Guides/Red_Teaming_TTPs.pdf" ) ;
45+ const pageBreak = "\n\n<div class=\"page-break\"></div>\n\n" ;
46+ const combinedMarkdown = (
47+ await Promise . all ( chapters . map ( ( f ) => fs . readFile ( f , "utf8" ) ) )
48+ ) . join ( pageBreak ) ;
4349
44- markdownpdf ( {
45- paperFormat : argv . paper ,
46- cssPath : argv . css ? path . resolve ( argv . css ) : undefined ,
47- } )
48- . concat . from . paths ( chapters )
49- . to ( outputPath , function ( err ) {
50- if ( err ) {
51- console . error ( "PDF generation failed:" , err ) ;
52- process . exit ( 1 ) ;
53- }
50+ await fs . mkdir ( path . resolve ( "Guides" ) , { recursive : true } ) ;
51+ const outputPath = path . resolve ( "Guides/Red_Teaming_TTPs.pdf" ) ;
52+
53+ try {
54+ await mdToPdf (
55+ { content : combinedMarkdown } ,
56+ {
57+ dest : outputPath ,
58+ stylesheet : argv . css ? [ path . resolve ( argv . css ) ] : undefined ,
59+ pdf_options : {
60+ format : argv . paper ,
61+ margin : "25mm" ,
62+ printBackground : true ,
63+ } ,
64+ launch_options : {
65+ args : [ "--no-sandbox" , "--disable-setuid-sandbox" ] , // for CI runners
66+ } ,
67+ }
68+ ) ;
5469 console . log ( `✓ PDF generated at ${ outputPath } ` ) ;
55- } ) ;
70+ } catch ( err ) {
71+ console . error ( "PDF generation failed:" , err ) ;
72+ process . exit ( 1 ) ;
73+ }
74+ } ) ( ) ;
0 commit comments