11const express = require ( 'express' ) ;
22const fs = require ( 'fs' ) ;
33const path = require ( 'path' ) ;
4+
45const { PDFNet } = require ( '@pdftron/pdfnet-node' ) ;
56const mimeType = require ( './modules/mimeType' ) ;
7+
68const filesPath = './files' ;
79
810const app = express ( ) ;
@@ -111,10 +113,36 @@ app.get('/convert/:filename', (req, res) => {
111113 const pdfdoc = await PDFNet . PDFDoc . create ( ) ;
112114 await pdfdoc . initSecurityHandler ( ) ;
113115 await PDFNet . Convert . toPdf ( pdfdoc , inputPath ) ;
114- pdfdoc . save (
115- outputPath ,
116- PDFNet . SDFDoc . SaveOptions . e_linearized ,
117- ) ;
116+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
117+ } ;
118+
119+ PDFNetEndpoint ( main , outputPath , res ) ;
120+ } ) ;
121+
122+ app . get ( '/convertHTML/:filename-:htmlPath' , ( req , res ) => {
123+ const filename = req . params . filename ;
124+ const htmlPath = req . params . htmlPath ;
125+
126+ const inputPath = path . resolve ( __dirname , filesPath , htmlPath ) ;
127+ const outputPath = path . resolve ( __dirname , filesPath , `${ filename } .pdf` ) ;
128+
129+ const main = async ( ) => {
130+ try {
131+ await PDFNet . HTML2PDF . setModulePath (
132+ path . resolve ( __dirname , './node_modules/@pdftron/pdfnet-node/lib/' ) ,
133+ ) ;
134+ const settings = await PDFNet . HTML2PDF . WebPageSettings . create ( ) ;
135+ settings . setAllowJavaScript ( true ) ;
136+ settings . setProduceForms ( true ) ;
137+ const html2pdf = await PDFNet . HTML2PDF . create ( ) ;
138+ const pdfdoc = await PDFNet . PDFDoc . create ( ) ;
139+ await html2pdf . insertFromUrl2 ( inputPath , settings ) ;
140+ await html2pdf . convert ( pdfdoc ) ;
141+ await pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
142+ } catch ( err ) {
143+ console . log ( err ) ;
144+ }
145+
118146 } ;
119147
120148 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -129,10 +157,7 @@ app.get('/generate/:filename', (req, res) => {
129157 await pdfdoc . initSecurityHandler ( ) ;
130158 const page1 = await pdfdoc . pageCreate ( ) ;
131159 pdfdoc . pagePushBack ( page1 ) ;
132- pdfdoc . save (
133- outputPath ,
134- PDFNet . SDFDoc . SaveOptions . e_linearized ,
135- ) ;
160+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
136161 } ;
137162
138163 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -149,7 +174,11 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
149174 }
150175
151176 const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
152- const outputPath = path . resolve ( __dirname , filesPath , `${ filename } -${ pageNumber } .txt` ) ;
177+ const outputPath = path . resolve (
178+ __dirname ,
179+ filesPath ,
180+ `${ filename } -${ pageNumber } .txt` ,
181+ ) ;
153182
154183 const main = async ( ) => {
155184 await PDFNet . initialize ( ) ;
@@ -168,7 +197,7 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
168197 let text ;
169198
170199 text = await txt . getAsText ( ) ;
171- fs . writeFile ( outputPath , text , ( err ) => {
200+ fs . writeFile ( outputPath , text , err => {
172201 if ( err ) return console . log ( err ) ;
173202 } ) ;
174203 } catch ( err ) {
@@ -181,10 +210,14 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
181210
182211app . get ( '/replaceContent/:name' , ( req , res ) => {
183212 const name = req . params . name . replace ( '_' , ' ' ) ;
184- const filename = 'template_letter.pdf'
213+ const filename = 'template_letter.pdf' ;
185214
186215 const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
187- const outputPath = path . resolve ( __dirname , filesPath , `${ filename } _replaced.pdf` ) ;
216+ const outputPath = path . resolve (
217+ __dirname ,
218+ filesPath ,
219+ `${ filename } _replaced.pdf` ,
220+ ) ;
188221
189222 const main = async ( ) => {
190223 const pdfdoc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
@@ -197,10 +230,7 @@ app.get('/replaceContent/:name', (req, res) => {
197230 await replacer . addString ( 'DATE' , new Date ( Date . now ( ) ) . toLocaleString ( ) ) ;
198231 await replacer . process ( page ) ;
199232
200- pdfdoc . save (
201- outputPath ,
202- PDFNet . SDFDoc . SaveOptions . e_linearized ,
203- ) ;
233+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
204234 } ;
205235
206236 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -217,7 +247,11 @@ app.get('/watermark/:filename-:watermark', (req, res) => {
217247 }
218248
219249 const inputPath = path . resolve ( __dirname , filesPath , filename ) ;
220- const outputPath = path . resolve ( __dirname , filesPath , `${ filename } _watermarked.pdf` ) ;
250+ const outputPath = path . resolve (
251+ __dirname ,
252+ filesPath ,
253+ `${ filename } _watermarked.pdf` ,
254+ ) ;
221255
222256 const main = async ( ) => {
223257 const pdfdoc = await PDFNet . PDFDoc . createFromFilePath ( inputPath ) ;
@@ -234,13 +268,13 @@ app.get('/watermark/:filename-:watermark', (req, res) => {
234268 ) ;
235269 const redColorPt = await PDFNet . ColorPt . init ( 1 , 0 , 0 ) ;
236270 stamper . setFontColor ( redColorPt ) ;
237- const pgSet = await PDFNet . PageSet . createRange ( 1 , await pdfdoc . getPageCount ( ) ) ;
271+ const pgSet = await PDFNet . PageSet . createRange (
272+ 1 ,
273+ await pdfdoc . getPageCount ( ) ,
274+ ) ;
238275 stamper . stampText ( pdfdoc , watermark , pgSet ) ;
239276
240- pdfdoc . save (
241- outputPath ,
242- PDFNet . SDFDoc . SaveOptions . e_linearized ,
243- ) ;
277+ pdfdoc . save ( outputPath , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
244278 } ;
245279
246280 PDFNetEndpoint ( main , outputPath , res ) ;
@@ -261,7 +295,7 @@ const PDFNetEndpoint = (main, pathname, res) => {
261295 }
262296 } ) ;
263297 } )
264- . catch ( ( error ) => {
298+ . catch ( error => {
265299 res . statusCode = 500 ;
266300 res . end ( error ) ;
267301 } ) ;
0 commit comments