Skip to content

Commit

Permalink
Changed the rendering to puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
luxedo committed Dec 15, 2018
1 parent 1125270 commit 0ad93f7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# prettycode
`prettycode` is a software to pretty print code into a `pdf` file.

![example](prettycode_example.png)

```
Usage: prettycode [-h | -v | -s style | -e encoding | -l language | -a message | -o output_folder] FILES
Converts code into a pretty formated pdf with syntax highlight
Expand All @@ -18,6 +16,13 @@ Arguments:
-o, --output Chooses the output folder. Default: .
```

Running the command passing itself as argument prints the source code:
```sh
./prettycode -a "<b>Preetycode Source</b>" prettycode
```
![example](prettycode_example.png)


## Dependencies

* [node](https://nodejs.org/en/)
Expand All @@ -29,6 +34,7 @@ Just install the node dependencies and you are good to go
git clone [email protected]:luxedo/prettycode.git
cd prettycode
npm install
./prettycode FILES
```

## License
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "prettycode.js",
"dependencies": {
"highlight.js": "^9.13.1",
"html-pdf": "^2.2.0",
"language-detect": "^1.1.0",
"markdown-it": "^8.4.2",
"optimist": "^0.6.1"
"optimist": "^0.6.1",
"puppeteer": "^1.11.0"
},
"devDependencies": {},
"scripts": {
Expand Down
88 changes: 46 additions & 42 deletions prettycode
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ const hljs = require("highlight.js");
const markdown = require("markdown-it");
const path = require("path");
const pdf = require("html-pdf");
const puppeteer = require("puppeteer");

let args = require('optimist').argv;
let args = require("optimist").argv;
let help = `Usage: prettycode [-h | -v | -s style | -e encoding | -l language | -a message | -o output_folder] FILES
Converts code into a pretty formated pdf with syntax highlight
Expand Down Expand Up @@ -68,10 +69,10 @@ var md = markdown({
html: false,
xhtmlOut: false,
breaks: false,
langPrefix: 'language-',
langPrefix: "language-",
linkify: false,
typographer: false,
quotes: '“”‘’',
quotes: "“”‘’",
highlight: function(str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
Expand All @@ -95,45 +96,48 @@ args._.map(file => {
}
let code = md.render(`\`\`\`${language}\n${data}\n\`\`\`\n\n`);
let html = `
<head>
<link rel="stylesheet" type="text/css" href="file://${__dirname}/node_modules/highlight.js/styles/${style}.css">
</head>
<style>
body, pre, code {
font-size: 7px;
overflow: hidden !important;
}
</style>
<body>
${code.trim()}
</body>`;
let options = {
"phantomPath": `${__dirname}/node_modules/phantomjs-prebuilt/bin/phantomjs`,
"phantomArgs": [],
"timeout": 30000,
"border": {
"top": "1in", // default is 0, units: mm, cm, in, px
"right": "0.5in",
"bottom": "1in",
"left": "0.5in"
},
};
if (header !== "") {
options.border.top = 0;
options.header = {
"height": "1in",
"contents": `<br/>${header}`
};
}
if (footer !== "") {
options.border.bottom = 0;
options.footer = {
"height": "1in",
"contents": `${footer}`
<html>
<body>
${code.trim()}
</body>
</html>`;

/* jshint ignore:start */
(async () => {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
const page = await browser.newPage();
await page.setContent(html);
let tag = (string) => `<span style="font-size: 10px; width: 800px; height: 200px; color: #333; margin: 20px; margin-left: 50px">${string}</span>`
let options = {
path: `${output}/${fileName}.pdf`,
format: "A4",
margin: {
top: "20mm",
right: "15mm",
bottom: "25mm",
left: "15mm",
},
displayHeaderFooter: header!==""||footer!=="",
headerTemplate: tag(header),
footerTemplate: tag(footer),
};
}
pdf.create(html, options).toFile(`${output}/${fileName}.pdf`, function(err, res) {
if (err) return console.log(err);
});
await page.addStyleTag({path: `${__dirname}/node_modules/highlight.js/styles/${style}.css`});
await page.addStyleTag({content: `
@media print {
pre {
white-space: pre-wrap;
}
}
html {
-webkit-print-color-adjust: exact;
font-size: 10px;
}`
});
await page.pdf(options);
await browser.close();
})();
/* jshint ignore:end */
});
});
Binary file modified prettycode_example.pdf
Binary file not shown.
Binary file modified prettycode_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ad93f7

Please sign in to comment.