Skip to content

Commit

Permalink
tweak pdf generation using custom css
Browse files Browse the repository at this point in the history
- center images and captions
- prevent images to exceed page width
  • Loading branch information
marph91 committed Apr 29, 2023
1 parent 6eda619 commit 06d96e7
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/note_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ def parse_args():
return parser.parse_args()


def create_custom_css_file(path):
with open(path, "w") as outfile:
outfile.write(
"""
img {
/* prevent image going out of page */
max-width: 100%;
/* center image */
display: block;
margin-left: auto;
margin-right: auto;
}
figure > figcaption{
text-align: center;
}
"""
)


def main():
args = parse_args()

Expand Down Expand Up @@ -95,6 +114,7 @@ def main():
f"Valid formats: {valid_output_formats}."
)
# special arguments for some output formats
create_custom_css_file(Path(tmpdirname) / "custom.css")
format_kwargs = {
# https://github.com/NicklasTegner/pypandoc/issues/186#issuecomment-673282133
"pdf": {
Expand All @@ -104,6 +124,8 @@ def main():
"weasyprint",
"--metadata",
f"title={candidate.title}",
"--css",
Path(tmpdirname) / "custom.css",
],
}
}
Expand Down

0 comments on commit 06d96e7

Please sign in to comment.