annals.pagepdf#
Every published html page carries its own Download PDF button, and the PDF beside it.
A page is often read without the annals page around it: opened full page, its raw url
mailed on, or shared. So at publish time the stored html itself gets a small “Download
PDF” link (hidden when printing), pointing at <stem>.pdf in the same document:
The page opts out (
data-annals-pdf="off"on<html>or<body>, orannals publish --no-pdf): nothing is added.The page has its own button (any element with
data-annals-pdf): nothing is added; it already says where its PDF is.The page declares its PDF (
<link rel="alternate" type="application/pdf" href="...">): the button points there, and nothing is printed.The document already holds
<stem>.pdfbeside the page: the button points at it.Otherwise the page is printed to
<stem>.pdfbypdf_renderer(defaultprint_with_chromium(), pdfdol’s headless Chromium, in thepdfextra). When itcannot print (no Chromium, a page that will not load in time), the button calls the browser’s own print dialog instead, so it is never missing.
What this adds is one block wrapped in <!--annals-pdf--> markers (pure ASCII, so it
suits a page in any charset), so publishing a page that already went through here (a new
version through --replace) swaps the old button for a new one and prints the new page
again. The page’s bytes are otherwise kept as they were, whatever their encoding.
Module Attributes
|
|
the longest a page may take to print, in seconds, before the button prints instead |
|
|
what |
"the default renderer", told apart from |
Functions
|
what |
|
|
|
Give the staged html page |
|
Where the button annals put in |
|
The href of the page's own |
|
Print an html file to PDF with headless Chromium (pdfdol), with its print css. |
|
|
|
False when the page opts out, or already has a PDF button of its own. |
- annals.pagepdf.DFLT = 'default'#
“the default renderer”, told apart from
None(never print)
- annals.pagepdf.DFLT_PDF_RENDERER(page, *, timeout=60)#
what
attach_pdf()prints with when not told; read at call time, so a test or an embedding app can swap it once- Return type:
- annals.pagepdf.DFLT_PRINT_TIMEOUT_S = 60#
the longest a page may take to print, in seconds, before the button prints instead
- annals.pagepdf.PdfRenderer#
path of a staged html page -> PDF bytes; raises when it cannot print
- annals.pagepdf.add_button(page, href)[source]#
pagewith a Download PDF link tohref(or a print button whenNone).One block goes right after
<body>(or where the body would start): a<link rel="alternate">declaring the PDF, so readers and agents find it, its style, and the button. Any button added earlier is replaced.hrefis used as given (quote a file name first:attach_pdf()does).>>> page = "<!doctype html><html><head></head><body><p>x</p></body></html>" >>> out = add_button(page, "x.pdf") >>> 'href="x.pdf"' in out and 'type="application/pdf"' in out True >>> out.startswith("<!doctype html>") True >>> "window.print()" in add_button("<p>x</p>", None) True
- Return type:
- annals.pagepdf.attach_pdf(root, main, files, *, pdf_renderer='default')[source]#
Give the staged html page
root/mainits PDF button (and its PDF).Returns
(files, pdf):files(relative toroot) gains<stem>.pdfwhen it is printed here, andpdfis where the page’s PDF is (a file of the document, or the url the page declared),Nonewhen there is none.pdf_rendererdefaults toDFLT_PDF_RENDERER;Nonenever prints: the button links to a PDF the document or the page already has, else opens the print dialog. A renderer that fails is logged and treated the same way; publishing never fails for want of a PDF.
- annals.pagepdf.button_href(page)[source]#
Where the button annals put in
pagepoints (None: none, or a print button).>>> button_href(add_button("<p>x</p>", "x.pdf")) 'x.pdf'
- annals.pagepdf.declared_pdf(page)[source]#
The href of the page’s own
<link rel="alternate" type="application/pdf">.A link annals added itself does not count: that is not the page declaring one.
>>> declared_pdf('<link rel="alternate" type="application/pdf" href="guide.pdf">') 'guide.pdf' >>> declared_pdf('<link rel="stylesheet" href="a.css">') is None True
- annals.pagepdf.print_with_chromium(page, *, timeout=60)[source]#
Print an html file to PDF with headless Chromium (pdfdol), with its print css.
Needs the
pdfextra andplaywright install chromium. It runs in a child process, killed (with its browser) aftertimeoutseconds. Images markedloading="lazy"would print blank, so the page is printed from a copy without that attribute, beside it so its relative files still load.- Return type:
- annals.pagepdf.strip_button(page)[source]#
pagewithout whatadd_button()put in it.>>> strip_button(add_button("<body><p>x</p></body>", "x.pdf")) '<body><p>x</p></body>'
- Return type:
- annals.pagepdf.wants_button(page)[source]#
False when the page opts out, or already has a PDF button of its own.
>>> wants_button("<p>plain</p>") True >>> wants_button('<body data-annals-pdf="off"><p>x</p></body>') False >>> wants_button('<a href="g.pdf" data-annals-pdf>PDF</a>') False >>> wants_button('<script>"<a data-annals-pdf>"</script><p data-annals-pdf-note=1>') True
- Return type: