Skip to content

Commit 8514080

Browse files
author
Andrey
committed
Adding HTML conversion API
1 parent 6e73823 commit 8514080

File tree

7 files changed

+192
-24
lines changed

7 files changed

+192
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# node-modules
2-
node_modules/
2+
node_modules/
3+
pdfnet-node

app.js

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

182207
app.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
});

files/index.html

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<html>
2+
<head>
3+
<meta name="viewport" content="width=device-width, initial-scale=1">
4+
<style>
5+
body {
6+
font-family: Arial, Helvetica, sans-serif;
7+
background-color: black;
8+
}
9+
10+
* {
11+
box-sizing: border-box;
12+
}
13+
14+
/* Add padding to containers */
15+
.container {
16+
padding: 16px;
17+
background-color: white;
18+
}
19+
20+
/* Full-width input fields */
21+
input[type=text], input[type=password] {
22+
width: 100%;
23+
padding: 15px;
24+
margin: 5px 0 22px 0;
25+
display: inline-block;
26+
border: none;
27+
background: #f1f1f1;
28+
}
29+
30+
input[type=text]:focus, input[type=password]:focus {
31+
background-color: #ddd;
32+
outline: none;
33+
}
34+
35+
/* Overwrite default styles of hr */
36+
hr {
37+
border: 1px solid #f1f1f1;
38+
margin-bottom: 25px;
39+
}
40+
41+
/* Set a style for the submit button */
42+
.registerbtn {
43+
background-color: #4CAF50;
44+
color: white;
45+
padding: 16px 20px;
46+
margin: 8px 0;
47+
border: none;
48+
cursor: pointer;
49+
width: 100%;
50+
opacity: 0.9;
51+
}
52+
53+
.registerbtn:hover {
54+
opacity: 1;
55+
}
56+
57+
/* Add a blue text color to links */
58+
a {
59+
color: dodgerblue;
60+
}
61+
62+
/* Set a grey background color and center the text of the "sign in" section */
63+
.signin {
64+
background-color: #f1f1f1;
65+
text-align: center;
66+
}
67+
</style>
68+
</head>
69+
<body>
70+
71+
<form action="/action_page.php">
72+
<div class="container">
73+
<h1>Register</h1>
74+
<p>Please fill in this form to create an account.</p>
75+
<hr>
76+
77+
<label for="email"><b>Email</b></label>
78+
<input type="text" placeholder="Enter Email" name="email" id="email" required>
79+
80+
<label for="psw"><b>Password</b></label>
81+
<input type="password" placeholder="Enter Password" name="psw" id="psw" required>
82+
83+
<label for="psw-repeat"><b>Repeat Password</b></label>
84+
<input type="password" placeholder="Repeat Password" name="psw-repeat" id="psw-repeat" required>
85+
<hr>
86+
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
87+
88+
<button type="submit" class="registerbtn">Register</button>
89+
</div>
90+
91+
<div class="container signin">
92+
<p>Already have an account? <a href="#">Sign in</a>.</p>
93+
</div>
94+
</form>
95+
96+
</body>
97+
</html>

files/myhtml.pdf

Whitespace-only changes.

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "nodemon server.js",
8+
"postinstall": "node tools/copy-pdfnet-lib.js",
89
"test": "mocha --timeout 10000"
910
},
1011
"author": "",
@@ -16,6 +17,7 @@
1617
"nodemon": "^2.0.3"
1718
},
1819
"devDependencies": {
20+
"fs-extra": "^7.0.1",
1921
"chai": "^4.2.0",
2022
"chai-http": "^4.3.0",
2123
"mocha": "^7.1.1"

tools/copy-pdfnet-lib.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require('fs-extra');
2+
3+
const copyFiles = async () => {
4+
try {
5+
await fs.copy('./node_modules/@pdftron/pdfnet-node/lib', './pdfnet-node/lib');
6+
console.log('PDFNet files copied over successfully');
7+
} catch (err) {
8+
console.error(err);
9+
}
10+
};
11+
12+
copyFiles();

0 commit comments

Comments
 (0)