Start rendering PDF and HTML to output
This commit is contained in:
parent
ca8b97bdb0
commit
ededd98e77
8 changed files with 63 additions and 9 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -86,3 +86,6 @@ ENV/
|
||||||
|
|
||||||
# Rope project settings
|
# Rope project settings
|
||||||
.ropeproject
|
.ropeproject
|
||||||
|
|
||||||
|
out/
|
||||||
|
md_pdf/assets/cover.html
|
||||||
|
|
10
md_pdf/assets/cover-template.html
Normal file
10
md_pdf/assets/cover-template.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="style.css" />
|
||||||
|
</head>
|
||||||
|
<body class="cover">
|
||||||
|
<h1>{{ title }}</h1>
|
||||||
|
<h3>{{ subtitle }}</h3>
|
||||||
|
</body>
|
||||||
|
</html>
|
8
md_pdf/assets/footer.html
Normal file
8
md_pdf/assets/footer.html
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body class="footer">
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
8
md_pdf/assets/header.html
Normal file
8
md_pdf/assets/header.html
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="style.css" />
|
||||||
|
</head>
|
||||||
|
<body class="header">
|
||||||
|
</body>
|
||||||
|
</html>
|
9
md_pdf/assets/style.css
Normal file
9
md_pdf/assets/style.css
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
body.cover {
|
||||||
|
margin: 0 auto;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.cover h1 {
|
||||||
|
font-size: 72px;
|
||||||
|
padding-top: 400px;
|
||||||
|
}
|
|
@ -1,7 +1,8 @@
|
||||||
import click
|
import click
|
||||||
from md_pdf.build.md import read_files
|
from md_pdf.build.md import read_files
|
||||||
from md_pdf.build.pandoc import build_document
|
from md_pdf.build.pandoc import build_document, output_html
|
||||||
from md_pdf.build.cover import render_cover
|
from md_pdf.build.cover import render_cover
|
||||||
|
from md_pdf.build.pdf import export_pdf
|
||||||
|
|
||||||
|
|
||||||
@click.command('build', short_help="Build document")
|
@click.command('build', short_help="Build document")
|
||||||
|
@ -11,6 +12,9 @@ from md_pdf.build.cover import render_cover
|
||||||
def cli(in_files, bibliography, output):
|
def cli(in_files, bibliography, output):
|
||||||
data = read_files(in_files)
|
data = read_files(in_files)
|
||||||
doc = build_document(data, bibliography)
|
doc = build_document(data, bibliography)
|
||||||
|
output_html(doc, output)
|
||||||
render_cover()
|
render_cover()
|
||||||
|
export_pdf(doc, output)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,11 @@ def fix_references_title(content):
|
||||||
return soup.prettify()
|
return soup.prettify()
|
||||||
|
|
||||||
|
|
||||||
|
def output_html(html, out_dir):
|
||||||
|
with open(os.path.join(out_dir, 'output.html'), 'w') as f:
|
||||||
|
f.write(html)
|
||||||
|
|
||||||
|
|
||||||
def build_document(files_content, bibliography):
|
def build_document(files_content, bibliography):
|
||||||
args = [
|
args = [
|
||||||
'-s',
|
'-s',
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
import pdfkit
|
import pdfkit
|
||||||
|
from md_pdf.utils import PROJECT_DIR
|
||||||
|
from md_pdf.build.cover import OUTPUT_COVER_FILE
|
||||||
|
import os
|
||||||
|
|
||||||
pdf_options = {
|
|
||||||
|
STYLE_FILE = os.path.join(PROJECT_DIR, 'assets', 'style.css')
|
||||||
|
HEADER_FILE = os.path.join(PROJECT_DIR, 'assets', 'header.html')
|
||||||
|
FOOTER_FILE = os.path.join(PROJECT_DIR, 'assets', 'footer.html')
|
||||||
|
PDF_OPTIONS = {
|
||||||
"quiet": "",
|
"quiet": "",
|
||||||
"no-pdf-compression": "",
|
"no-pdf-compression": "",
|
||||||
|
|
||||||
|
@ -9,8 +16,8 @@ pdf_options = {
|
||||||
"margin-left": '0.4in',
|
"margin-left": '0.4in',
|
||||||
"margin-right": '0.4in',
|
"margin-right": '0.4in',
|
||||||
|
|
||||||
"header-html": "header.html",
|
"header-html": HEADER_FILE,
|
||||||
"footer-html": "footer.html",
|
"footer-html": FOOTER_FILE,
|
||||||
"footer-spacing": 5,
|
"footer-spacing": 5,
|
||||||
"header-spacing": 5,
|
"header-spacing": 5,
|
||||||
|
|
||||||
|
@ -21,11 +28,11 @@ pdf_options = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def export_pdf(content):
|
def export_pdf(content, out_dir):
|
||||||
return pdfkit.from_string(
|
return pdfkit.from_string(
|
||||||
content,
|
content,
|
||||||
'out.pdf',
|
os.path.join(out_dir, 'output.pdf'),
|
||||||
options=pdf_options,
|
options=PDF_OPTIONS,
|
||||||
css="style.css",
|
css=STYLE_FILE,
|
||||||
cover="cover.html"
|
cover=OUTPUT_COVER_FILE
|
||||||
)
|
)
|
||||||
|
|
Reference in a new issue