First test to run pagedjs grom python.

master
Gijs 4 years ago
commit dbe02f7ead

@ -0,0 +1,6 @@
# Grafting a tree
# Installation
The prototype uses pagedjs-cli to convert the HTML into PDF. In a terminal `cd` into the cloned folder and run:
`npm install pagedjs-cli`

@ -0,0 +1,18 @@
class delayedLoading extends Paged.Handler {
// this let us call the methods from the the chunker, the polisher and the caller for the rest of the script
constructor(chunker, polisher, caller) {
super(chunker, polisher, caller);
}
beforeParsed (_) {
document.body.appendChild(document.createTextNode('this text was inserted before the slow resolve'));
return new Promise(function (resolve) {
setTimeout(function () {
document.body.appendChild(document.createTextNode('this text was inserted before pagedjs is loaded'));
resolve();
}, 2000);
});
}
}
Paged.registerHandlers(delayedLoading);

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello world</title>
<style>
h1 {
string-set: runningheader content(text);
page-break-after: right;
}
h2, h3, h4, h5, h6 {
page-break-inside: avoid;
page-break-after: avoid;
}
p {
widows: 3;
orphans: 3;
}
a {
string-set: lastlink attr(href);
}
@page {
/* width and height. In this case for an A5 */
size: 148.5mm 210mm;
/* Bigger bleed as weasyprint draws the cropmarks within the bleed*/
@top-center {
content: "Hello world example file";
}
@bottom-center {
content: counter(page);
}
@top-left {
color: red;
font-size: 80%;
content: string(runningheader);
}
@top-right {
content: string(lastlink);
}
}
@page:left {
/* Only applied on left pages */
margin-left: 40mm;
}
@page:right {
/* Only applied on right pages */
margin-right: 40mm;
}
</style>
</head>
<body>
<h1>
Hello world.
</h1>
<p>This document started as a <a href="http://osp.kitchen">website</a>.</p>
<p>This is a second paragraph.</p>
</body>
</html>

@ -0,0 +1,82 @@
import subprocess
import tempfile
import os.path
basepath = os.path.abspath(os.path.dirname(__file__))
paged_bin = 'node_modules/pagedjs-cli/bin/paged'
def run_pagedjs (path_html, path_pdf, cwd=None, extra_scripts=[]):
args = [
paged_bin
]
for script in extra_scripts:
args.extend([
'--additional-script',
script
])
args.extend([
'-o', path_pdf,
path_html
])
try:
return subprocess.check_output(args, cwd=cwd, stderr=subprocess.STDOUT).decode()
except subprocess.CalledProcessError as e:
return 'Error:\n{}'.format(e.output.decode())
"""
Generate a PDF based on provided HTML using pagedjs and returns the contents of
the generated PDF.
If optional path_out is provided the PDF is written there and the function returns the path.
Optional extra_scripts is a list of strings with javascript.
Scripts are sent in the same order to paged.js
"""
def make_pdf (html, path_out=None, extra_scripts=[]):
with tempfile.TemporaryDirectory(prefix='algoliterary_publishing_house_') as tempdir:
with tempfile.NamedTemporaryFile(dir=tempdir, mode='w', suffix='.html', delete=False) as temphtml:
# Store html in a temporary file
temphtml.write(html)
temphtml.close()
name_in = temphtml.name
extra_scripts_tmp = []
for script in extra_scripts:
with tempfile.NamedTemporaryFile(dir=tempdir, mode='w', suffix='.js', delete=False) as tempjs:
tempjs.write(script)
tempjs.close()
extra_scripts_tmp.append(tempjs.name)
# Make a temporary file for the generated PDF
with tempfile.NamedTemporaryFile(dir=tempdir, mode='w', suffix='.pdf', delete=False) as temppdf:
temppdf.close()
name_out = temppdf.name
# Make the pdf
run_pagedjs(name_in, name_out, cwd=basepath, extra_scripts=extra_scripts_tmp)
if path_out:
import shutil
shutil.copy(name_out, path_out)
return path_out
else:
with open(name_out) as generated_pdf:
return generated_pdf.read()
if __name__ == '__main__':
with open(os.path.join(basepath, 'index.html'), 'r') as file_input:
html = file_input.read()
with open(os.path.join(basepath, 'delayedLoading.js'), 'r') as js_input:
js = js_input.read()
make_pdf(html, os.path.join(basepath, 'generated.pdf'), [ js ])
Loading…
Cancel
Save