Skip to content

Commit

Permalink
Merge pull request #6 from andreysaf/adding-html-conversion
Browse files Browse the repository at this point in the history
Added HTML conversion
  • Loading branch information
andreysaf authored Oct 19, 2020
2 parents 6e73823 + 839e8c6 commit 12b2e6a
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# node-modules
node_modules/
node_modules/
pdfnet-node
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ The endpoint converts the file to a PDF. Conversion is possible for the followin
##### HTTP Request
`GET http://localhost:9000/convert/:filename`

### Convert to PDF from HTML

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).

##### HTTP Request
`GET http://localhost:9000/convertHTML/:filename-:pathToHTML`

##### Example
Converts an HTML form to a PDF
`http://localhost:9000/convertHTML/myhtml-index.html`

### Optimize PDF

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).
Expand Down
80 changes: 57 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const express = require('express');
const fs = require('fs');
const path = require('path');

const { PDFNet } = require('@pdftron/pdfnet-node');
const mimeType = require('./modules/mimeType');

const filesPath = './files';

const app = express();
Expand Down Expand Up @@ -111,10 +113,36 @@ app.get('/convert/:filename', (req, res) => {
const pdfdoc = await PDFNet.PDFDoc.create();
await pdfdoc.initSecurityHandler();
await PDFNet.Convert.toPdf(pdfdoc, inputPath);
pdfdoc.save(
outputPath,
PDFNet.SDFDoc.SaveOptions.e_linearized,
);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};

PDFNetEndpoint(main, outputPath, res);
});

app.get('/convertHTML/:filename-:htmlPath', (req, res) => {
const filename = req.params.filename;
const htmlPath = req.params.htmlPath;

const inputPath = path.resolve(__dirname, filesPath, htmlPath);
const outputPath = path.resolve(__dirname, filesPath, `${filename}.pdf`);

const main = async () => {
try {
await PDFNet.HTML2PDF.setModulePath(
path.resolve(__dirname, './node_modules/@pdftron/pdfnet-node/lib/'),
);
const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
settings.setAllowJavaScript(true);
settings.setProduceForms(true);
const html2pdf = await PDFNet.HTML2PDF.create();
const pdfdoc = await PDFNet.PDFDoc.create();
await html2pdf.insertFromUrl2(inputPath, settings);
await html2pdf.convert(pdfdoc);
await pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
} catch (err) {
console.log(err);
}

};

PDFNetEndpoint(main, outputPath, res);
Expand All @@ -129,10 +157,7 @@ app.get('/generate/:filename', (req, res) => {
await pdfdoc.initSecurityHandler();
const page1 = await pdfdoc.pageCreate();
pdfdoc.pagePushBack(page1);
pdfdoc.save(
outputPath,
PDFNet.SDFDoc.SaveOptions.e_linearized,
);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};

PDFNetEndpoint(main, outputPath, res);
Expand All @@ -149,7 +174,11 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
}

const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(__dirname, filesPath, `${filename}-${pageNumber}.txt`);
const outputPath = path.resolve(
__dirname,
filesPath,
`${filename}-${pageNumber}.txt`,
);

const main = async () => {
await PDFNet.initialize();
Expand All @@ -168,7 +197,7 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {
let text;

text = await txt.getAsText();
fs.writeFile(outputPath, text, (err) => {
fs.writeFile(outputPath, text, err => {
if (err) return console.log(err);
});
} catch (err) {
Expand All @@ -181,10 +210,14 @@ app.get('/textExtract/:filename-:pagenumber', (req, res) => {

app.get('/replaceContent/:name', (req, res) => {
const name = req.params.name.replace('_', ' ');
const filename = 'template_letter.pdf'
const filename = 'template_letter.pdf';

const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(__dirname, filesPath, `${filename}_replaced.pdf`);
const outputPath = path.resolve(
__dirname,
filesPath,
`${filename}_replaced.pdf`,
);

const main = async () => {
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
Expand All @@ -197,10 +230,7 @@ app.get('/replaceContent/:name', (req, res) => {
await replacer.addString('DATE', new Date(Date.now()).toLocaleString());
await replacer.process(page);

pdfdoc.save(
outputPath,
PDFNet.SDFDoc.SaveOptions.e_linearized,
);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};

PDFNetEndpoint(main, outputPath, res);
Expand All @@ -217,7 +247,11 @@ app.get('/watermark/:filename-:watermark', (req, res) => {
}

const inputPath = path.resolve(__dirname, filesPath, filename);
const outputPath = path.resolve(__dirname, filesPath, `${filename}_watermarked.pdf`);
const outputPath = path.resolve(
__dirname,
filesPath,
`${filename}_watermarked.pdf`,
);

const main = async () => {
const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath);
Expand All @@ -234,13 +268,13 @@ app.get('/watermark/:filename-:watermark', (req, res) => {
);
const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
stamper.setFontColor(redColorPt);
const pgSet = await PDFNet.PageSet.createRange(1, await pdfdoc.getPageCount());
const pgSet = await PDFNet.PageSet.createRange(
1,
await pdfdoc.getPageCount(),
);
stamper.stampText(pdfdoc, watermark, pgSet);

pdfdoc.save(
outputPath,
PDFNet.SDFDoc.SaveOptions.e_linearized,
);
pdfdoc.save(outputPath, PDFNet.SDFDoc.SaveOptions.e_linearized);
};

PDFNetEndpoint(main, outputPath, res);
Expand All @@ -261,7 +295,7 @@ const PDFNetEndpoint = (main, pathname, res) => {
}
});
})
.catch((error) => {
.catch(error => {
res.statusCode = 500;
res.end(error);
});
Expand Down
Binary file modified files/document.docx.pdf
Binary file not shown.
97 changes: 97 additions & 0 deletions files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background-color: black;
}

* {
box-sizing: border-box;
}

/* Add padding to containers */
.container {
padding: 16px;
background-color: white;
}

/* Full-width input fields */
input[type=text], input[type=password] {
width: 100%;
padding: 15px;
margin: 5px 0 22px 0;
display: inline-block;
border: none;
background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
background-color: #ddd;
outline: none;
}

/* Overwrite default styles of hr */
hr {
border: 1px solid #f1f1f1;
margin-bottom: 25px;
}

/* Set a style for the submit button */
.registerbtn {
background-color: #4CAF50;
color: white;
padding: 16px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
opacity: 0.9;
}

.registerbtn:hover {
opacity: 1;
}

/* Add a blue text color to links */
a {
color: dodgerblue;
}

/* Set a grey background color and center the text of the "sign in" section */
.signin {
background-color: #f1f1f1;
text-align: center;
}
</style>
</head>
<body>

<form action="/action_page.php">
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>

<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" id="email" required>

<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" id="psw" required>

<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" id="psw-repeat" required>
<hr>
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>

<button type="submit" class="registerbtn">Register</button>
</div>

<div class="container signin">
<p>Already have an account? <a href="#">Sign in</a>.</p>
</div>
</form>

</body>
</html>
Binary file added files/myhtml.pdf
Binary file not shown.
Binary file modified files/new.pdf
Binary file not shown.
Binary file modified files/optimized_webviewer.pdf
Binary file not shown.
Binary file modified files/template_letter.pdf_replaced.pdf
Binary file not shown.
Binary file modified files/webviewer.pdf_watermarked.pdf
Binary file not shown.
Loading

0 comments on commit 12b2e6a

Please sign in to comment.