匯出 PDF
將文件頁面匯出為 PDF 檔案
概覽
一般而言,我們強烈建議您下載整個網站 (HTML 檔案等) 以便離線瀏覽。
若您想將頁面匯出為 PDF,可以參考本指南。
設定
使用 Puppeteer 匯出 PDF 檔案。
import puppeteer from 'puppeteer';
import fs from 'node:fs/promises';
import path from 'node:path';
const browser = await puppeteer.launch();
const outDir = 'pdfs';
// 請更新此處
const urls = ['/docs/ui', '/docs/ui/customizations'];
async function exportPdf(pathname: string) {
const page = await browser.newPage();
await page.goto('http://localhost:3000' + pathname, {
waitUntil: 'networkidle2',
});
await page.pdf({
path: path.join(outDir, pathname.slice(1).replaceAll('/', '-') + '.pdf'),
width: 950,
printBackground: true,
});
console.log(`PDF generated successfully for ${pathname}`);
await page.close();
}
await fs.mkdir(outDir, { recursive: true });
await Promise.all(urls.map(exportPdf));
await browser.close();請將以下內容加入您的 Tailwind CSS 檔案,以便在列印時隱藏導覽元素:
@media print {
#nd-docs-layout {
--fd-sidebar-width: 0px !important;
}
#nd-sidebar {
display: none;
}
}現在您可以執行指令碼:
bun ./scripts/export-pdf.ts隱藏的內容
若手風琴/頁籤中有隱藏的內容,您可能需要暫時覆寫 MDX 元件。例如:
import defaultMdxComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';
import { Accordion, Accordions } from 'fumadocs-ui/components/accordion';
// 您可以在此使用環境變數
const isPrinting = true;
function PrintingAccordion(props: React.ComponentProps<typeof Accordion>) {
return (
<div>
<h3>{props.title}</h3>
{props.children}
</div>
);
}
function PrintingAccordions(props: React.ComponentProps<typeof Accordions>) {
return <div>{props.children}</div>;
}
export function getMDXComponents(components?: MDXComponents) {
return {
...defaultMdxComponents,
// 更新後的手風琴元件:
Accordion: isPrinting ? PrintingAccordion : Accordion,
Accordions: isPrinting ? PrintingAccordions : Accordions,
...components,
} satisfies MDXComponents;
}How is this guide?
最後更新於
