pdfhttp

package
v1.139.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package pdfhttp serves an HTML document as a PDF printed by the platform's headless renderer (#1983).

Every route that serves a document's bytes at .../content has a .../pdf beside it. The PDF route reads the document through its own content route, in-process and with the caller's request, so who may export a document is exactly who may read it, by the same checks; then it prints the document in the renderer with its backgrounds, one page per slide for a deck, each slide at its last build step.

The print used to happen in the reader's browser, from a hidden frame. A browser prints backgrounds only when the reader asks, so a dark deck came out light text on white, and the fix for that repainted every deck light (#1772); a browser print view also lays out every build step of a slide as a page of its own. Far more decks are exported to be sent than to be printed, and the renderer prints what the reader sees.

Index

Constants

View Source
const (
	// DefaultTimeout bounds one print: loading the document, waiting for it
	// to lay itself out, and reading the PDF back.
	DefaultTimeout = 90 * time.Second
	// DefaultConcurrency is how many prints this replica runs at once. The
	// renderer is shared with the tile worker, and a print holds a browser
	// context for as long as the document takes to settle.
	DefaultConcurrency = 2

	// MaxDocumentBytes bounds the document a route reads to print. A slide
	// deck or a report is well under it; reading more than it would hold a
	// second copy of a large file in memory to refuse it.
	MaxDocumentBytes = 32 << 20
)
View Source
const PrintStep = `` /* 754-byte string literal not displayed */

PrintStep is added at the head of a document the renderer prints, so it runs before any script the document carries.

A deck is an HTML document that loads the slide runtime the platform serves (#1767). The runtime is a global the document's script tag assigns; the step takes that assignment through a property setter and, before the document initializes the runtime, queues a configure that selects the print view with every build step of a slide on one page (an option handed to configure before initialize is applied at initialization, below whatever the document passes to initialize itself) and a listener for the pdf-ready event the print view dispatches once every slide is a page. A document that never assigns the runtime is not a deck, and is ready once it has loaded and its fonts are in.

Either way the step resolves window.__pdfReady, which Ready waits on.

View Source
const Ready = `` /* 367-byte string literal not displayed */

Ready is the expression the renderer waits on before it prints: the step's promise, once the document has run far enough to create it.

Variables

View Source
var Routes = []Route{
	{Pattern: "GET /api/v1/portal/assets/{id}/pdf", Content: "/api/v1/portal/assets/{id}/content"},
	{Pattern: "GET /api/v1/portal/assets/{id}/versions/{version}/pdf", Content: "/api/v1/portal/assets/{id}/versions/{version}/content"},
	{Pattern: "GET /api/v1/admin/assets/{id}/pdf", Content: "/api/v1/admin/assets/{id}/content"},
	{Pattern: "GET /api/v1/admin/assets/{id}/versions/{version}/pdf", Content: "/api/v1/admin/assets/{id}/versions/{version}/content"},
	{Pattern: "GET /api/v1/resources/{id}/pdf", Content: "/api/v1/resources/{id}/content"},
	{Pattern: "GET /api/v1/resources/{id}/versions/{version}/pdf", Content: "/api/v1/resources/{id}/versions/{version}/content"},
	{Pattern: "GET /portal/view/{token}/pdf", Content: "/portal/view/{token}/content", Public: true},
	{Pattern: "GET /portal/view/{token}/items/{assetId}/pdf", Content: "/portal/view/{token}/items/{assetId}/content", Public: true},
}

Routes are the PDF routes, each beside the content route it prints.

Functions

func MountFor

func MountFor(mux *http.ServeMux, p *platform.Platform)

MountFor mounts the PDF routes on mux, printed by the renderer the platform's thumbnails section names, with public routes limited per client as the public viewer is. The routes read documents back through mux, so it is called once the mux is complete. With no renderer answering, a route says so rather than failing silently.

func WithPrintStep

func WithPrintStep(doc []byte) []byte

WithPrintStep is the document with PrintStep at its head.

The step goes just inside <head>, or <html> when there is no head tag, so the doctype the document opens with is still the first thing the parser sees; a document with neither tag takes the step first and is parsed as the browser parses any headless fragment.

Types

type Deps

type Deps struct {
	// Routes is the platform's assembled handler. The content route a PDF
	// route sits beside is read through it.
	Routes http.Handler
	// Printer is the headless renderer.
	Printer Printer
	// Timeout bounds one print; zero is DefaultTimeout.
	Timeout time.Duration
	// Concurrency is how many prints run at once; zero is
	// DefaultConcurrency.
	Concurrency int
	// Limiter admits a print on a public route; nil admits every one.
	Limiter interface {
		Allow(r *http.Request) bool
		RetryAfter() int
	}
}

Deps is what the routes print with.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler serves the PDF routes.

func New

func New(d Deps) *Handler

New returns the handler.

func (*Handler) Mount

func (h *Handler) Mount(mux *http.ServeMux)

Mount registers every PDF route on mux.

type Printer

type Printer interface {
	PrintPDF(ctx context.Context, p headless.Page) ([]byte, error)
}

Printer prints a page to PDF.

type Route

type Route struct {
	Pattern string
	Content string
	// Public is a route a caller reaches with no session, which is limited
	// per client: a print holds the renderer far longer than a read.
	Public bool
}

Route is one PDF route and the content route beside it whose document it prints. Content names the content route with the PDF route's wildcards.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL