Start rendering PDF and HTML to output

This commit is contained in:
Jake Howard 2017-03-28 21:59:11 +01:00
parent ca8b97bdb0
commit ededd98e77
8 changed files with 63 additions and 9 deletions

3
.gitignore vendored
View File

@ -86,3 +86,6 @@ ENV/
# Rope project settings
.ropeproject
out/
md_pdf/assets/cover.html

View 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>

View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body class="footer">
</body>
</html>

View 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
View File

@ -0,0 +1,9 @@
body.cover {
margin: 0 auto;
text-align: center;
}
body.cover h1 {
font-size: 72px;
padding-top: 400px;
}

View File

@ -1,7 +1,8 @@
import click
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.pdf import export_pdf
@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):
data = read_files(in_files)
doc = build_document(data, bibliography)
output_html(doc, output)
render_cover()
export_pdf(doc, output)
return 0

View File

@ -16,6 +16,11 @@ def fix_references_title(content):
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):
args = [
'-s',

View File

@ -1,6 +1,13 @@
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": "",
"no-pdf-compression": "",
@ -9,8 +16,8 @@ pdf_options = {
"margin-left": '0.4in',
"margin-right": '0.4in',
"header-html": "header.html",
"footer-html": "footer.html",
"header-html": HEADER_FILE,
"footer-html": FOOTER_FILE,
"footer-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(
content,
'out.pdf',
options=pdf_options,
css="style.css",
cover="cover.html"
os.path.join(out_dir, 'output.pdf'),
options=PDF_OPTIONS,
css=STYLE_FILE,
cover=OUTPUT_COVER_FILE
)