"""Writes a small PDF to disk, so that real readers can be pointed at it.
The Python twin of the `write_pdf` example in Rust: the same document, through
the binding rather than through the library directly.
Unit tests can only check that the bytes look right to us. Whether the file is
actually a PDF is a question only a PDF reader can answer, which is what
`scripts/check_pdf_validity.sh` uses this for.
No licence is set unless `--licensed` is passed, so what this writes is an
evaluation copy, watermarked on every page.
Usage: python examples/write_pdf.py [out.pdf] [--licensed]
"""
from __future__ import annotations
import sys
import _licence
import _out
import hqf_pdf
def invoice_page() -> hqf_pdf.Page:
"""A page of an invoice: a header block, a frame, and a run of table rules."""
content = hqf_pdf.Content()
content.save_state()
content.set_fill(hqf_pdf.Rgb(0.20, 0.40, 0.80))
content.rect(50.0, 700.0, 495.0, 90.0)
content.fill()
content.set_stroke(hqf_pdf.Rgb(0.0, 0.0, 0.0))
content.set_line_width(1.0)
content.rect(50.0, 50.0, 495.0, 742.0)
content.stroke()
content.set_line_width(0.4)
for row in range(24):
content.rect(50.0, 640.0 - row * 22.0, 495.0, 22.0)
content.stroke()
content.restore_state()
page = hqf_pdf.Page.a4()
page.set_content(content)
return page
def main() -> None:
licensed = "--licensed" in sys.argv
sys.argv = [arg for arg in sys.argv if arg != "--licensed"]
out = _out.output_path("out")
document = hqf_pdf.Document()
if licensed:
document.set_license(_licence.licensed())
document.set_info("Title", "hqf-pdf sample invoice")
document.set_info("Creator", "hqf-pdf")
document.add_page(invoice_page())
document.add_page(invoice_page())
written = document.write(out)
print(
f"wrote {out}: {written} bytes, {document.page_count} pages, "
f"{'licensed' if licensed else 'evaluation'}"
)
if __name__ == "__main__":
main()