Skip to content

PDF documents

Paperwork is a small web app that makes three PDF documents, the way a business needs them. Each document is an HTML page with CSS. Novis\Pdf\Document turns it into a PDF file.

The links below open the files that the app returns. You can look at them before you install anything.

The invoice has three pages. The table of items continues from one page to the next, and each page shows the amount carried forward. At the end, a banking app can read a QR code to pay the invoice.

The file follows PDF/A-3b, the standard for archived files. It also contains the same invoice as XML in the Factur-X format. An accounting program reads this XML, so nobody needs to type the invoice again.

Open the invoice (PDF)

The report has eight pages: a cover, a table of contents, key figures, charts and tables. It is confidential, so the file is encrypted. Your PDF viewer needs the password novis before it shows the report.

The file follows PDF/UA-1, the standard for accessible files. A screen reader can read it aloud, and each chart has a text that describes it.

Open the report (PDF)

This file is for the person who bought four tickets. Page 1 is a summary of the order. Then each ticket has its own page, with a QR code and a barcode. The app renders each ticket as its own PDF file. Then Pages::merge joins the five files into one.

Open the tickets (PDF)

Each tab shows one file of the app. These are the same files that you download below.

apps/paperwork/nvs.toml
# The server listens on port 8143. The other apps in this repository use other ports,
# so all of them can run at the same time.
[server]
listen = ["127.0.0.1:8143"]
# The app reads one file: the logo that the three documents show.
# The path is relative to this folder. The app may read no other file, and it may write none.
[capabilities.fs]
read = ["assets/logo.svg"]

Install Novis first. Then download only the folder apps/paperwork of the Novis repository. A sparse clone (a clone that checks out only the folders you name) does this:

Terminal window
git clone --depth 1 --filter=blob:none --sparse https://github.com/novis-lang/novis.git
cd novis
git sparse-checkout set apps/paperwork
cd apps/paperwork

Start the server from the folder apps/paperwork:

Terminal window
nvs serve public/index.nvs

Open http://127.0.0.1:8143/ in your browser. The home page has a link to each document. You can also open each address directly:

  • http://127.0.0.1:8143/invoice.pdf
  • http://127.0.0.1:8143/report.pdf
  • http://127.0.0.1:8143/tickets.pdf

The app renders the document again for each request. It writes no file.

Terminal window
nvs test .

nvs test . reads every .nvs file in the folder as one program and runs each #[Test] method. The tests open each finished PDF file with Novis\Pdf\Reader and check the text on its pages:

Paperwork\AppTest
✓ theHomePageLinksEachDocument 0.7 ms
✓ eachDocumentIsSentAsAPdf 589.4 ms
✓ anUnknownPathIsNotFound 0.3 ms
✓ aWrongMethodIsNotAllowed 0.2 ms
Paperwork\InvoiceTest
✓ theTotalsAddUp 0.5 ms
✓ theLastPageShowsTheTotal 172.7 ms
✓ theCarriedForwardAmountIsOnPageOne 177.3 ms
✓ theXmlHasTheSameNumberAndTotal 130.5 ms
✓ nothingIsDroppedOrCutOff 116.2 ms
✓ twoRendersGiveTheSameFile 263.6 ms
Paperwork\ReportTest
✓ theEncryptedReportCannotBeReadWithoutThePassword 261.0 ms
✓ theContentsGiveThePageOfEachChapter 230.0 ms
✓ theHeaderShowsTheChapter 256.9 ms
✓ theLongTableRepeatsItsHeader 183.2 ms
✓ nothingIsDroppedOrCutOff 207.5 ms
Paperwork\TicketsTest
✓ theOrderHasOnePageForEachTicket 165.4 ms
✓ eachTicketPageHasItsAttendee 297.2 ms
✓ nothingIsDroppedOrCutOff 248.1 ms
0 failed, 18 passed, 0 skipped, 0 flaky in 3305 ms
paperwork/
nvs.toml the configuration: the port, and the app may read its logo and no other file
assets/
logo.svg the logo that the three documents show
public/
index.nvs the entry file: every request starts here
src/
App.nvs the routes, and the method that sends a PDF file
AppTest.nvs sends requests to the app and checks the responses
Brand.nvs the logo, the colours and the money format of all three documents
Chart.nvs draws the charts of the report as SVG images
FacturX.nvs writes the XML of the electronic invoice
Gate.nvs decides which routes may run, and shows the error pages
Home.nvs the HTML pages: a link to each document, and the error pages
Invoice.nvs the invoice
InvoiceTest.nvs tests the totals, the pages and the XML of the invoice
Report.nvs the quarterly report
ReportTest.nvs tests the password, the contents and the tables of the report
Sample.nvs the data that the documents show
Tickets.nvs the tickets, and the summary of the order
TicketsTest.nvs tests the pages of the tickets
Totals.nvs the sums of the invoice, for the PDF and for the XML

nvs.toml has two sections. [server] listen sets the port of the server to 8143. The other example apps use other ports, so you can run them at the same time. [capabilities.fs] allows the app to read the file assets/logo.svg. The app cannot read any other file, and it cannot write a file.

public/index.nvs calls Core\Router::dispatch(new Paperwork\Gate()). dispatch finds the route that matches the request and calls its method.

src/App.nvs has one method for each route. send() sets the header content-disposition: inline, so the browser shows the PDF file. Then it sends the bytes with Core\Response::sendBytes.

src/Gate.nvs is the gate, and dispatch calls it for every request. allows runs before each route method. refused shows a page when no route can handle the request, such as a path with no route (404) or a wrong HTTP method (405). failed shows a page when a route method throws an error (500). dispatch sets the status code first, so the gate only shows the page.

Invoice.nvs, Report.nvs and Tickets.nvs each build the HTML of their document with an html template, which escapes every value. Document::fromHtml reads that HTML. Each method call after it adds one thing to the document, and render() returns the finished file.

Each document sets its creation time with withCreationTime(). The same data then always gives the same file, byte for byte. The test twoRendersGiveTheSameFile checks this.

Sample.nvs has the data of the three documents. A real app reads it from its database.

The invoice:

  • Document::fromHtml reads the HTML and the CSS of the invoice.
  • withAsset adds the logo and the QR code as SVG images. The HTML shows them with <img src="logo.svg">.
  • withHeader and withFooter build the top and the bottom of each page. Each gets a Page, which has the page number and the number of pages.
  • withSlots fills the rows “Carried forward” and “Brought forward” on each page. Each row of items has a data-mark attribute with its amount.
  • Page::marks returns the amounts on one page. Page::marksUpTo returns the amounts on this page and on every page before it.
  • withStandard with Document::Standard::PdfA3b makes a file for an archive.
  • withInvoiceXml puts the Factur-X XML into the file.
  • withCreationTime sets the creation time.
  • QrCode::giroCode writes the text of a SEPA bank transfer. QrCode::svg draws that text as a QR code.

The report:

  • withPassword encrypts the file with a password.
  • withStandard with Document::Standard::PdfUa1 makes an accessible file. Each chart has an alt text, which a screen reader reads.
  • Page::string returns the name of the chapter for the header. Each chapter heading saves its text with the CSS property string-set.
  • The cover is on a page named cover, from the CSS rule @page cover. The header checks $page->name, and the cover gets no header.
  • The table of contents uses the CSS function target-counter(). It gives the page number of each chapter.
  • withAsset adds the two charts. Chart.nvs draws each chart as SVG, so the PDF file has it as a vector drawing.

The tickets:

  • Document::fromHtml and render make one PDF file for each ticket, and one for the summary.
  • Pages::of reads each file. Pages::merge joins them into one file, in order.
  • QrCode::svg and Barcode::svg draw the two codes of each ticket. The barcode uses Barcode::Symbology::Code128.

The tests:

  • Reader::open opens a finished file. text returns the text of one page. invoiceXml returns the XML inside the invoice.
  • Rendered has the bytes of the file, its number of pages and its overflows. overflows lists the content that did not fit on its page. The tests check that this list is empty.
  1. Copy the folder apps/paperwork and give it the name of your app.
  2. Choose a namespace for your app, for example Shop. Change namespace Paperwork; in each file under src/, and the autoload line and Paperwork\Gate in public/index.nvs.
  3. Keep the document that is closest to yours, for example Invoice.nvs, and its test. Delete the other documents, their tests and their routes in src/App.nvs.
  4. Replace Sample.nvs with your own data. Then change the HTML and the CSS. Run nvs test . after each change.
  5. Replace assets/logo.svg with your logo. If the file name changes, change it in nvs.toml too.

The home page of this app shows the password of the report, so you can open the file. A real app gets the password from the person who requests the report.

  • Novis\Pdf\Document lists every method that builds a PDF file.
  • Notes is an example app with SQLite, HTML pages and a JSON API.