Skip to content

Commit 12b2e6a

Browse files
author
Andrey Safonov
authored
Merge pull request #6 from andreysaf/adding-html-conversion
Added HTML conversion
2 parents 6e73823 + 839e8c6 commit 12b2e6a

13 files changed

+250
-99
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

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ The endpoint converts the file to a PDF. Conversion is possible for the followin
5454
##### HTTP Request
5555
`GET http://localhost:9000/convert/:filename`
5656

57+
### Convert to PDF from HTML
58+
59+
The endpoint converts the HTML to a PDF. There are several settings available to get the best results. Uses [PDFTron Node.js API for HTML2PDF](https://www.pdftron.com/documentation/samples/node/js/HTML2PDFTest?platforms=nodejs).
60+
61+
##### HTTP Request
62+
`GET http://localhost:9000/convertHTML/:filename-:pathToHTML`
63+
64+
##### Example
65+
Converts an HTML form to a PDF
66+
`http://localhost:9000/convertHTML/myhtml-index.html`
67+
5768
### Optimize PDF
5869

5970
The endpoint converts the PDF to an optimized PDF to be used with [PDFTron WebViewer](https://www.pdftron.com/webviewer/demo/). Uses [PDFTron Node.js API](https://www.pdftron.com/api/pdfnet-node/PDFNet.PDFDoc.html#saveViewerOptimized__anchor).

app.js

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const express = require('express');
22
const fs = require('fs');
33
const path = require('path');
4+
45
const { PDFNet } = require('@pdftron/pdfnet-node');
56
const mimeType = require('./modules/mimeType');
7+
68
const filesPath = './files';
79

810
const 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

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

files/document.docx.pdf

932 Bytes
Binary file not shown.

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

15 KB
Binary file not shown.

files/new.pdf

60 Bytes
Binary file not shown.

files/optimized_webviewer.pdf

0 Bytes
Binary file not shown.
152 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)