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
|
||||
.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
|
||||
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
|
||||
|
||||
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
Reference in a new issue