62 lines
3.2 KiB
TypeScript
62 lines
3.2 KiB
TypeScript
export const printDocument = (htmlContent: string, docTitle: string = '图文报告') => {
|
|
const iframe = document.createElement('iframe');
|
|
iframe.style.position = 'fixed';
|
|
iframe.style.right = '0';
|
|
iframe.style.bottom = '0';
|
|
iframe.style.width = '0';
|
|
iframe.style.height = '0';
|
|
iframe.style.border = '0';
|
|
document.body.appendChild(iframe);
|
|
|
|
const win = iframe.contentWindow;
|
|
const doc = iframe.contentDocument || win?.document;
|
|
if (doc && win) {
|
|
doc.open();
|
|
doc.write(`
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<style>
|
|
@page { size: A4; margin: 15mm 10mm; }
|
|
* { box-sizing: border-box; }
|
|
body { margin: 0; padding: 0; font-family: SimSun, "Microsoft YaHei", serif; color: #1E293B; background: white; }
|
|
.content { width: 100%; min-height: 277mm; margin: 0 auto; }
|
|
img { max-width: 100%; height: auto; display: block; margin: 8px auto; }
|
|
p { margin: 0; padding: 0; line-height: 1.5; }
|
|
h1 { font-size: 20px; margin: 16px 0 12px; font-weight: 600; text-align: center; }
|
|
strong, b { font-weight: 600; }
|
|
u { text-decoration: underline; }
|
|
table { width: 100%; border-collapse: collapse; margin: 16px 0; table-layout: fixed; }
|
|
td { padding: 8px; border: 1px solid #e2e8f0; vertical-align: top; }
|
|
.image-placeholder { border: 2px dashed #cbd5e1; border-radius: 8px; padding: 16px; margin-bottom: 8px; background: #f8fafc; min-height: 70px; display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative; }
|
|
.image-placeholder.has-image { border: none; background: transparent; padding: 0; min-height: 0; }
|
|
.delete-btn { display: none !important; }
|
|
.image-placeholder:not(.has-image) { display: none !important; }
|
|
.template-info-section { position: relative; margin-bottom: 16px; }
|
|
.smart-field-wrapper { display: inline-flex; align-items: center; margin: 0 2px; vertical-align: text-bottom; }
|
|
.smart-field-wrapper .field-label { color: #64748b; user-select: none; }
|
|
.smart-field-wrapper .field-value { min-width: 32px; padding: 0 4px; margin: 0 2px; border: 1px solid #cbd5e1; border-radius: 2px; display: inline-block; background: #f8fafc; color: #0f172a; line-height: 1.2; font-size: inherit; vertical-align: text-bottom; box-sizing: border-box; min-height: 1.2em; outline: none; }
|
|
.report-signature-img { max-width: 120px; max-height: 40px; width: auto; height: auto; object-fit: contain; vertical-align: middle; display: inline-block; }
|
|
@media print {
|
|
.smart-field-wrapper .field-value { border: none !important; border-bottom: 1px solid #000 !important; border-radius: 0 !important; background: transparent !important; padding: 0 2px !important; }
|
|
.smart-field-wrapper .field-value.no-underline { border-bottom: none !important; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="content">${htmlContent}</div>
|
|
</body>
|
|
</html>
|
|
`);
|
|
doc.close();
|
|
win.focus();
|
|
setTimeout(() => {
|
|
win.print();
|
|
setTimeout(() => {
|
|
if (iframe.parentNode) document.body.removeChild(iframe);
|
|
}, 1000);
|
|
}, 300);
|
|
}
|
|
};
|