Die Python-Datei des Beispiels „Ein ganzer Roman auf Englisch“.
Ein gemeinfreier englischer Roman, in seine zwölf Kapitel eingelesen und über zweiundsechzig nummerierte Seiten gesetzt, hinter einem anklickbaren Inhaltsverzeichnis, jedes Kapitel auf einer eigenen Seite beginnend, und die Engine wählt jeden Zeilenumbruch.
Python
59 Zeilen
Wofür dieses Beispiel da ist
Die meisten Beispiele auf dieser Website sind eine Seite lang, und das sagt nichts darüber, was bei Seite sechzig geschieht. Hier geht der ganze Text von Lewis Carrolls Alice's Adventures in Wonderland, 1865 erschienen, genau so hinein, wie die reine Textdatei ihn enthält, und kommt als ein PDF heraus: eine Titelseite, ein Inhaltsverzeichnis, zwölf Kapitel, zweiundsechzig nummerierte Seiten. Dem Programm wird über diesen Text eine einzige Sache gesagt — dass eine Zeile CHAPTER I ein Kapitel eröffnet —, und jede Überschrift, jeder Kolumnentitel und jede Seitenzahl folgt daraus.
Was dieses Beispiel zeigt
Ein ganzes Buch, als reiner Text übergeben, ohne Layoutanweisungen.
Zwölf im Text selbst gefundene Kapitel, jedes beginnt auf einer eigenen Seite.
Zweiundsechzig nummerierte Seiten hinter einem anklickbaren Inhaltsverzeichnis.
"""Lays a whole English novel out and writes it as one PDF.The Python twin of the `write_english_book` example in Rust.It is what a document's weight really looks like. A novel draws the wholealphabet in both cases, the digits and the punctuation, so the embedded fontcarries far more outlines than an invoice does -- and its metrics, which used tobe stated for every glyph of the face, are stated only for the glyphs the bookdraws.Its twin, `write_french_book`, lays another novel out through the same code. Neitheris a translation of the other: each is a book of its own, and each says how its ownfile marks a chapter.The book opens two columns of pages wide, the odd pages on the right, as a printed booklies open.Usage: python examples/write_english_book.py [out.pdf] [font.ttf]"""from__future__importannotationsimport_bookimport_licenceimport_outimporthqf_pdfBOOK=_book.Book(title="Alice's Adventures in Wonderland",author="Lewis Carroll",file="wonderland.txt",mark_before="CHAPTER ",mark_after=".",chapters=12,contents_title="Contents",)defmain()->None:out=_out.output_path("english_book")document=hqf_pdf.Document()document.set_license(_licence.licensed())handle=document.add_font(hqf_pdf.Font.from_path(_out.font_path()))text=BOOK.path().read_text(encoding="utf-8")_book.lay_out(document,handle,BOOK,text)document.set_page_layout(hqf_pdf.PageLayout.TwoColumnsOddRight)data=document.to_bytes()out.parent.mkdir(parents=True,exist_ok=True)out.write_bytes(data)print(f"wrote {out}: {len(data)} bytes")if__name__=="__main__":main()