Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions blog/2026-11-21-clickable-pdf-preview/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
slug: clickable-pdf-preview-html
title: How to Turn a PDF Page Into a Clickable Preview in HTML
description: Save one PDF page as a JPG and link the preview to the full document in HTML. Try it with a downloadable two-page café menu and a working website example.
image: /tutorials/media/clickable-pdf-preview.jpg
date: 2026-11-21
authors:
- key: charly
name: Charly Abraham
tags: [Tools, HTML]
---

Show the first page of a PDF as a small picture on your website. When a visitor clicks the picture or the words below it, they get the complete document.

Try the café menu on your phone or computer. The preview shows **Breakfast & lunch**. Tap it to find **Drinks & sweets** on page two. <a href="/tutorials/pdf-menu-preview/cafe-menu.html">Open the finished café page</a>.

<!-- truncate -->

## Start with the practice files

You'll need a browser and a text editor. <a href="/tutorials/pdf-menu-preview.zip" download>Download the café menu project</a> and extract the ZIP. Open the folder that contains `cafe-menu.html`, `menu.pdf` and `menu-preview.jpg`. To make the preview yourself, copy just `cafe-menu.html` and `menu.pdf` into a new folder. You'll add the JPG there next.

For your own website, use the HTML page you want visitors to see and put your PDF beside it. You can also <a href="/tutorials/pdf-menu-preview/menu.pdf" download="menu.pdf">download the two-page practice PDF</a> on its own.

## How to save PDF as JPG in your browser

1. Open the [Phoenix Code PDF to JPG Converter](https://phcode.dev/tools/pdf-to-jpg.html). Select **Choose PDF** and open `menu.pdf`. Two page previews appear.
2. Under **Resolution**, choose **Custom**. Set **Size by** to **Pixels**, enter `600` in **Width (px)** and leave **Height (px)** empty. The converter keeps the proportions of your page.
3. Under **Pages to export**, enter `1`. Check that the tool says **1 of 2 pages selected**. Only the breakfast page should be checked.
4. Select **Convert selected pages**. When page one says **Ready to download**, select its **Download JPG** button. The practice file downloads as `menu-page-1.jpg`, at 600 × 800 pixels.
5. Rename the download to `menu-preview.jpg` and move it beside `cafe-menu.html` and `menu.pdf`. Open the JPG to check that it shows the café name and the breakfast menu.

<a href="/tutorials/media/pdf-menu-page-selection.png"><img src="/tutorials/media/pdf-menu-page-selection.png" width="356" height="356" style={{height: 'auto'}} loading="lazy" alt="Page 1 is checked and ready to download as a 600 × 800 JPG. Page 2 is present and unchecked." /></a>

## Link the image and its caption to the PDF

Open `cafe-menu.html` in your text editor. In the practice file, find this link just below `</header>`. On your own page, insert it inside `<body>` where the preview should appear.

```html
<a class="menu-link" href="menu.pdf">
<img src="menu-preview.jpg" alt="" width="600" height="800">
<span>Read the full menu (PDF)</span>
</a>
```

`src` names the picture the browser shows. `href` names the file the link opens. If your document is called `lunch-menu.pdf`, use `href="lunch-menu.pdf"` and keep `src` pointed at your JPG. Change the caption to tell visitors which document opens.

Because the picture and the visible caption are inside the same link, a keyboard user reaches them in one tab stop. The caption already names the destination, so the image has an empty `alt=""`. [W3C guidance on images within links](https://www.w3.org/WAI/tutorials/images/functional/#logo-image-within-link-text).

The practice page already has the following layout rules. On your own page, add them to your stylesheet or inside a `<style>` element in `<head>`. If you use a different JPG, update the HTML width and height to match its pixel dimensions.

```css
.menu-link {
display: inline-flex;
flex-direction: column;
gap: 0.75rem;
width: fit-content;
max-width: 100%;
}

.menu-link img {
display: block;
width: 18rem;
max-width: 100%;
height: auto;
}
```

## Check the full menu

Save `cafe-menu.html` and open it in your browser. Click the picture. Depending on your browser settings, the PDF opens in a viewer or downloads. Open the file and check that page two contains **Drinks & sweets** and **Lemon cake**. Go back to the HTML page and try the caption. With a keyboard, press **Tab** until the menu link has a focus outline, then press **Enter**.

If the caption works but the picture is missing, compare `src` with the JPG's filename and folder. If the picture appears but the PDF fails to open, check `href` against the PDF's name. When you put the page online, include all three files and test the live link from your phone. For a link to another HTML page, see [Link to another page in Phoenix Code](/docs/tutorials/link-pages).
2 changes: 2 additions & 0 deletions docs/10-Tutorials/02-link-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ You should return to the home page. Your link works in both directions because t

Use the separate browser window to check how a visitor will use the page.

An image can link to a document, too. [Make a clickable PDF preview with a two-page café menu](/blog/clickable-pdf-preview-html).

## 5. Try one more change

Change the link to open the About page instead. Use `about.html` as the destination and `About me` as the text. Save and test it again.
Expand Down
2 changes: 1 addition & 1 deletion scripts/package-tutorials.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('node:path');
const {zipSync} = require('fflate');

const TUTORIALS = path.join(__dirname, '../static/tutorials');
const PROJECTS = ['first-website', 'bootstrap-starter', 'image-text-layout'];
const PROJECTS = ['first-website', 'bootstrap-starter', 'image-text-layout', 'pdf-menu-preview'];

function projectFiles(folder, relative = '') {
const files = {};
Expand Down
2 changes: 1 addition & 1 deletion scripts/verify-tutorials.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const BLOGS = ['how-to-convert-png-to-pdf-free-online', 'how-to-convert-png-to-p
'test-website-on-different-screen-sizes', 'get-hex-color-from-image', 'convert-webp-to-jpg-background',
'decode-base64-text-and-images', 'change-all-caps-website-text', 'change-svg-fill-and-stroke',
'make-a-markdown-table', 'responsive-css-grid-sidebar', 'fix-trailing-comma-json',
'unix-timestamp-seconds-or-milliseconds', 'convert-word-to-markdown'];
'unix-timestamp-seconds-or-milliseconds', 'convert-word-to-markdown', 'clickable-pdf-preview-html'];
const ARTICLE_ROUTES = [...GUIDES.map(guide => '/docs/' + guide), ...BLOGS.map(slug => '/blog/' + slug)];
const WATCH_VIDEOS = [...Object.values(imageVideos), ...Object.values(pngPdfVideos), ...Object.values(layoutVideos),
...Object.values(htmlPreviewVideos)];
Expand Down
Binary file added static/tutorials/media/clickable-pdf-preview.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/tutorials/pdf-menu-preview.zip
Binary file not shown.
61 changes: 61 additions & 0 deletions static/tutorials/pdf-menu-preview/cafe-menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Alder Street Café menu</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
background: #fffcf6;
color: #1d3d37;
font: 1rem/1.55 system-ui, sans-serif;
}
main {
display: grid;
align-items: center;
gap: 2rem;
max-width: 52rem;
margin: 0 auto;
padding: clamp(1.5rem, 6vw, 4rem);
}
h1 {
margin: 0 0 1rem;
font: normal clamp(2.5rem, 6vw, 4rem)/1.05 Georgia, serif;
}
p { max-width: 29ch; margin: 0; }
.menu-link {
display: inline-flex;
flex-direction: column;
gap: 0.75rem;
width: fit-content;
max-width: 100%;
color: #85402b;
text-underline-offset: 0.2em;
}
.menu-link img {
display: block;
width: 18rem;
max-width: 100%;
height: auto;
}
.menu-link:focus-visible { outline: 3px solid currentColor; outline-offset: 5px; }
@media (min-width: 44rem) {
main { grid-template-columns: 1fr 18rem; }
}
</style>
</head>
<body>
<main>
<header>
<h1>Alder Street Café</h1>
<p>Breakfast and lunch are on the first page. Open the menu for drinks and sweets.</p>
</header>
<a class="menu-link" href="menu.pdf">
<img src="menu-preview.jpg" alt="" width="600" height="800">
<span>Read the full menu (PDF)</span>
</a>
</main>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/tutorials/pdf-menu-preview/menu.pdf
Binary file not shown.
Loading