Le fichier Python de l'exemple « Un roman entier en anglais ».
Un roman anglais du domaine public lu en ses douze chapitres et mis en page sur soixante-deux pages numérotées, derrière une table des matières où l'on peut cliquer, chaque chapitre ouvrant sa propre page et le moteur choisissant chaque coupure de ligne.
Python
54 lignes
À quoi sert cet exemple
La plupart des exemples de ce site tiennent sur une page, ce qui ne dit rien de ce qui se passe à la page soixante. Ici, tout Alice au pays des merveilles de Lewis Carroll, publié en 1865, entre tel que le fichier texte le contient et ressort en un seul PDF : une page de titre, un sommaire, douze chapitres, soixante-deux pages numérotées. Le programme reçoit une seule information sur ce texte — une ligne qui dit CHAPTER I ouvre un chapitre — et tous les titres, tous les en-têtes courants et tous les numéros de page en découlent.
Ce que montre cet exemple
Un livre entier remis en texte brut, sans la moindre consigne de mise en page.
Douze chapitres trouvés dans le texte lui-même, chacun ouvrant sa propre page.
Soixante-deux pages numérotées derrière une table des matières où l'on clique.
"""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.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)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()