適用:使用者在頁面上填幾個欄位,頁面排出一張卡片,按一下存成 PNG 圖片——活動海報、感謝狀、名牌、成果分享圖、流程圖存圖放進簡報。
要 PDF 或要印出來:用瀏覽器的列印(window.print(),另存成 PDF),不用這份。
先講清楚三件事
- 圖片只在這台電腦產生,不會送到平台;這份做法不存資料。但存下來的圖片會被轉傳(LINE、Email):圖上不要放病人的資料。使用者要做的圖有病人資料,先回到分級問診照實回答(
wanfangai_classify),不要自己判斷能不能做。 - 用平台書架的 html2canvas-pro,不要從 CDN 載入原版 html2canvas:原版 1.4.1 遇到新的顏色寫法(
oklch()、color-mix())會整個失敗。維護版用法一樣,函式也叫html2canvas()。 - 卡片上的東西只能來自工具資料夾或平台:照片、logo 放進工具資料夾;字型用書架的(官方做法 fonts)。別的網站的圖片會被安全設定擋,存出來的圖那裡是空白。
平台給的東西
https://platform.wanfangai.tw/lib/[email protected]/html2canvas-pro.min.js,載入後是全域函式html2canvas。- 不用在應用宣告(
app-manifest.yml)寫任何東西。
做法
<!doctype html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>感謝狀產生器|教學部</title>
<link rel="stylesheet" href="https://platform.wanfangai.tw/lib/[email protected]/400.css">
<link rel="stylesheet" href="https://platform.wanfangai.tw/lib/[email protected]/400.css">
<script src="https://platform.wanfangai.tw/lib/[email protected]/html2canvas-pro.min.js"></script>
<style>
body { margin: 0 auto; max-width: 760px; padding: 16px; font-family: 'Noto Sans TC', system-ui, sans-serif; }
label { display: block; margin: 8px 0; }
input { font-size: 18px; padding: 6px; width: 100%; box-sizing: border-box; }
button { font-size: 18px; padding: 10px 20px; margin: 12px 0; }
/* The card: plain styles only (background, border, rounded corners, pictures, text). */
#card { width: 600px; max-width: 100%; aspect-ratio: 4 / 3; box-sizing: border-box; padding: 40px; background: #fffdf5;
border: 8px double #b08d3c; border-radius: 12px; text-align: center; font-family: 'LXGW WenKai TC', 'Noto Serif TC', serif; }
#card h2 { font-size: 48px; font-weight: 400; margin: 8px 0 24px; letter-spacing: 0.3em; }
#card .name { font-size: 36px; margin: 16px 0; }
#card p { font-size: 20px; line-height: 1.8; margin: 4px 0; }
#result img { max-width: 100%; border: 1px solid #ccc; }
</style>
</head>
<body>
<h1>感謝狀產生器</h1>
<label>姓名 <input id="name" value="王小明"></label>
<label>感謝事由 <input id="reason" value="協助新進人員臨床教學"></label>
<label>日期 <input id="date" value="中華民國 115 年 12 月 1 日"></label>
<div id="card">
<h2>感謝狀</h2>
<div class="name" id="card-name"></div>
<p id="card-reason"></p>
<p>特頒此狀,以資感謝。</p>
<p id="card-date"></p>
</div>
<button id="save">存成圖片</button>
<div id="result"></div>
<script>
const card = document.getElementById('card');
function fill() {
// textContent, never HTML: a name typed with < > stays text.
document.getElementById('card-name').textContent = document.getElementById('name').value;
document.getElementById('card-reason').textContent = `感謝您${document.getElementById('reason').value},`;
document.getElementById('card-date').textContent = document.getElementById('date').value;
}
for (const id of ['name', 'reason', 'date']) document.getElementById(id).addEventListener('input', fill);
fill();
document.getElementById('save').addEventListener('click', async () => {
// Fonts arrive after the page opens; drawing before they do gives the computer's own font.
await document.fonts.load("48px 'LXGW WenKai TC'", card.textContent);
await document.fonts.ready;
const canvas = await html2canvas(card, { scale: 2, backgroundColor: '#ffffff', useCORS: true });
const blob = await new Promise((done) => canvas.toBlob(done, 'image/png'));
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `感謝狀-${document.getElementById('name').value || '未命名'}.png`;
a.click();
// Phones: show the picture too, so it can be saved with a long press.
const img = document.createElement('img');
img.src = url;
img.alt = '存好的圖片(手機:長按圖片存到相簿)';
document.getElementById('result').replaceChildren(img);
});
</script>
</body>
</html>scale: 2讓圖片是畫面的兩倍大,放進簡報或印出來才清楚;要印成海報用3。- 卡片的大小在 CSS 裡寫死(上面是 600px 寬、4:3),每台電腦存出來的圖才一樣大;要正方形改
aspect-ratio: 1。 - 照片與 logo:
<img src="images/logo.png" alt="…">,檔案放進工具資料夾。 - 流程圖、圖表也能用同一招存圖:對放圖的
<div>呼叫html2canvas()(流程圖見官方做法 flowchart,圖表見 chart-dashboard)。
常見的坑
| 看到什麼 | 為什麼 | 怎麼辦 |
|---|---|---|
| 存出來的字是電腦內建的字 | 字型還沒下載完就存 | 照「做法」先等字型 |
| 圖片模糊 | 沒加 scale | scale: 2,印刷用 3 |
| 圖片裡照片的位置是空白 | 照片從別的網站來(被安全設定擋),或從平台來但沒加 useCORS: true | 照片放進工具資料夾 |
| 陰影、漸層、濾鏡、特殊效果沒出現在圖裡 | html2canvas 是照著畫面重畫一次,不是截圖,有些 CSS 效果畫不出來 | 卡片用簡單的樣式:底色、框線、圓角、圖片、文字 |
主控台寫 Attempting to parse an unsupported color function | 載入的是原版 html2canvas | 換成書架的 html2canvas-pro |
| 手機按了沒下載 | 有些手機瀏覽器不支援直接下載 | 頁面下方會出現圖片,長按存到相簿 |
說「做好了」之前
- 發佈後打開:瀏覽器主控台沒有紅字;改欄位,卡片跟著變;按「存成圖片」有下載一個 PNG,打開來字型對、不模糊、沒有被切掉;手機上也試一次(下方出現圖片,長按存得起來)。
- 確認不了的,不要說它已經可以用,改成請使用者打開確認哪幾項。