@@ -111,10 +111,34 @@ app.get('/convert/:filename', (req, res) => {
111111 const pdfdoc = await PDFNet . PDFDoc . create ( ) ;
112112 await pdfdoc . initSecurityHandler ( ) ;
113113 await PDFNet . Convert . toPdf ( pdfdoc , inputPath ) ;
114- pdfdoc . save (
115- outputPath ,
116- PDFNet . SDFDoc . SaveOptions . e_linearized ,
117- ) ;
114+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
115+ } ;
116+
117+ PDFNetEndpoint ( main , outputPath , res ) ;
118+ } ) ;
119+
120+ app . get ( '/convertHTML/:filename-:htmlPath' , ( req , res ) => {
121+ const filename = req . params . filename ;
122+ const htmlPath = req . params . htmlPath ;
123+
124+ const inputPath = path . resolve ( __dirname , filesPath , htmlPath ) ;
125+ const outputPath = path . resolve ( __dirname , filesPath , `${ filename } .pdf` ) ;
126+
127+ const main = async ( ) => {
128+ try {
129+ await PDFNet . HTML2PDF . setModulePath (
130+ path . resolve ( __dirname , './pdfnet-node/lib/' ) ,
131+ ) ;
132+ const html2pdf = await PDFNet . HTML2PDF . create ( ) ;
133+ const pdfdoc = await PDFNet . PDFDoc . create ( ) ;
134+ await pdfdoc . initSecurityHandler ( ) ;
135+ await html2pdf . insertFromUrl ( 'http://www.gutenberg.org/wiki/Main_Page' ) ;
136+ await html2pdf . convert ( pdfdoc ) ;
137+ await pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
138+ } catch ( err ) {
139+ console . log ( err ) ;
140+ }
141+
118142 } ;
119143
120144 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -129,10 +153,7 @@ app.get('/generate/:filename', (req, res) => {
129153 await pdfdoc . initSecurityHandler ( ) ;
130154 const page1 = await pdfdoc . pageCreate ( ) ;
131155 pdfdoc . pagePushBack ( page1 ) ;
132- pdfdoc . save (
133- outputPath ,
134- PDFNet . SDFDoc . SaveOptions . e_linearized ,
135- ) ;
156+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
136157 } ;
137158
138159 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -149,7 +170,11 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
149170 }
150171
151172 const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
152- const outputPath = path . resolve ( __dirname , filesPath , `${ filename } -${ pageNumber } .txt` ) ;
173+ const outputPath = path . resolve (
174+ __dirname ,
175+ filesPath ,
176+ `${ filename } -${ pageNumber } .txt` ,
177+ ) ;
153178
154179 const main = async ( ) => {
155180 await PDFNet . initialize ( ) ;
@@ -168,7 +193,7 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
168193 let text ;
169194
170195 text = await txt . getAsText ( ) ;
171- fs . writeFile ( outputPath , text , ( err ) => {
196+ fs . writeFile ( outputPath , text , err => {
172197 if ( err ) return console . log ( err ) ;
173198 } ) ;
174199 } catch ( err ) {
@@ -181,10 +206,14 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
181206
182207app . get ( '/replaceContent/:name' , ( req , res ) => {
183208 const name = req . params . name . replace ( '_' , ' ' ) ;
184- const filename = 'template_letter.pdf'
209+ const filename = 'template_letter.pdf' ;
185210
186211 const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
187- const outputPath = path . resolve ( __dirname , filesPath , `${ filename } _replaced.pdf` ) ;
212+ const outputPath = path . resolve (
213+ __dirname ,
214+ filesPath ,
215+ `${ filename } _replaced.pdf` ,
216+ ) ;
188217
189218 const main = async ( ) => {
190219 const pdfdoc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
@@ -197,10 +226,7 @@ app.get('/replaceContent/:name', (req, res) => {
197226 await replacer . addString ( 'DATE' , new Date ( Date . now ( ) ) . toLocaleString ( ) ) ;
198227 await replacer . process ( page ) ;
199228
200- pdfdoc . save (
201- outputPath ,
202- PDFNet . SDFDoc . SaveOptions . e_linearized ,
203- ) ;
229+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
204230 } ;
205231
206232 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -217,7 +243,11 @@ app.get('/watermark/:filename-:watermark', (req, res) => {
217243 }
218244
219245 const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
220- const outputPath = path . resolve ( __dirname , filesPath , `${ filename } _watermarked.pdf` ) ;
246+ const outputPath = path . resolve (
247+ __dirname ,
248+ filesPath ,
249+ `${ filename } _watermarked.pdf` ,
250+ ) ;
221251
222252 const main = async ( ) => {
223253 const pdfdoc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
@@ -234,13 +264,13 @@ app.get('/watermark/:filename-:watermark', (req, res) => {
234264 ) ;
235265 const redColorPt = await PDFNet . ColorPt . init ( 1 , 0 , 0 ) ;
236266 stamper . setFontColor ( redColorPt ) ;
237- const pgSet = await PDFNet . PageSet . createRange ( 1 , await pdfdoc . getPageCount ( ) ) ;
267+ const pgSet = await PDFNet . PageSet . createRange (
268+ 1 ,
269+ await pdfdoc . getPageCount ( ) ,
270+ ) ;
238271 stamper . stampText ( pdfdoc , watermark , pgSet ) ;
239272
240- pdfdoc . save (
241- outputPath ,
242- PDFNet . SDFDoc . SaveOptions . e_linearized ,
243- ) ;
273+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
244274 } ;
245275
246276 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -261,7 +291,7 @@ const PDFNetEndpoint = (main, pathname, res) => {
261291 }
262292 } ) ;
263293 } )
264- . catch ( ( error ) => {
294+ . catch ( error => {
265295 res . statusCode = 500 ;
266296 res . end ( error ) ;
267297 } ) ;
0 commit comments