Let a blog post be downloaded as a PDF

Download PDF beside the byline opens the browser's print dialog (Save
as PDF) with a print layout: the site's header, footer and notices
hidden, the contents column dropped, the light palette whatever the
screen shows, and at the bottom the post's link and a QR code of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 15:34:43 -05:00
parent c03914a210
commit 5dceeb5860
4 changed files with 61 additions and 7 deletions

View file

@ -21,6 +21,9 @@ import { postDate, useBlogPost } from "../lib/useBlog.ts"
import { accentText, BRAND } from "../lib/color.ts"
import PostContents from "../components/PostContents.tsx"
import { headingAnchors } from "../lib/contents.ts"
import QrCode from "../components/QrCode.tsx"
import { siteUrl } from "../lib/site.ts"
import { FilePdf } from "@phosphor-icons/react"
const BODY = "var(--color-muted)"
@ -49,6 +52,7 @@ export default function BlogPost() {
const contents = post.show_contents ? headingAnchors(post.blocks).entries : []
const withContents = contents.length >= 2
const postUrl = siteUrl(`/blog/${post.id}`)
const date = postDate(post.published_on)
const cover = mediaUrl(post.cover_image)
const byline = [
@ -85,7 +89,7 @@ export default function BlogPost() {
<div
className={
withContents
? "mx-auto grid max-w-6xl gap-8 px-6 lg:grid-cols-[14rem_minmax(0,1fr)] lg:gap-12"
? "mx-auto grid max-w-6xl gap-8 px-6 lg:grid-cols-[14rem_minmax(0,1fr)] lg:gap-12 print:block"
: "contents"
}
>
@ -117,9 +121,17 @@ export default function BlogPost() {
{tag}
</span>
))}
<button
type="button"
onClick={() => window.print()}
className="ml-auto inline-flex items-center gap-1.5 rounded-full border border-link px-3 py-1 font-semibold text-link transition-colors hover:bg-tint print:hidden"
>
<FilePdf size={16} weight="bold" aria-hidden="true" />
Download PDF
</button>
<Link
to={BACK.to}
className="ml-auto hover:underline"
className="hover:underline print:hidden"
style={{ color: accentText(BRAND) }}
>
{BACK.label}
@ -135,6 +147,17 @@ export default function BlogPost() {
)}
<ContentBlocks blocks={post.blocks} accent={BRAND} />
{/* Only on paper (Download PDF): where the post lives online,
as a link and as a code to scan. */}
<footer className="hidden break-inside-avoid items-center gap-6 border-t border-brand/20 pt-6 print:flex">
<QrCode value={postUrl} label={`QR code for ${postUrl}`} className="h-28 w-28 shrink-0" />
<p className="text-sm text-body">
Read this post online at
<br />
<span className="font-semibold text-link">{postUrl}</span>
</p>
</footer>
</article>
</div>
),