forked from Gitlink/gitlink_help_center
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import React from 'react';
|
||
|
||
export default function PdfExportButton({ title }) {
|
||
const handleExport = async () => {
|
||
const element = document.getElementById('doc-markdown-content');
|
||
if (element) {
|
||
// 动态导入 html2pdf.js,只在客户端执行
|
||
const html2pdf = (await import('html2pdf.js')).default;
|
||
html2pdf()
|
||
.set({
|
||
margin: 0.5,
|
||
filename: `${title}.pdf`,
|
||
html2canvas: { scale: 2 },
|
||
jsPDF: { unit: 'in', format: 'a4', orientation: 'portrait' }
|
||
})
|
||
.from(element)
|
||
.save();
|
||
} else {
|
||
alert('未找到文档内容区域');
|
||
}
|
||
};
|
||
|
||
return (
|
||
<button
|
||
onClick={handleExport}
|
||
style={{
|
||
fontSize: 14,
|
||
padding: '0 16px',
|
||
background: '#466aff',
|
||
color: '#fff',
|
||
borderRadius: 4,
|
||
height: 32,
|
||
lineHeight: '32px',
|
||
border: 'none',
|
||
cursor: 'pointer',
|
||
display: 'inline-block',
|
||
marginLeft: 12,
|
||
boxSizing: 'border-box',
|
||
}}
|
||
>
|
||
导出PDF
|
||
</button>
|
||
);
|
||
} |