问题描述
我正在尝试使用“html-pdf”包在 nodejs 中创建一个 pdf 文件。
首先我导入为 import pdf from "html-pdf"
并收到此错误:
_htmlPdf.default.create(...).foFile is not a function.
但是当我导入 import { create } from "html-pdf"
时,我在控制台中收到此错误:
TypeError: (0,_htmlPdf.create)(...).foFile is not a function
这是我使用的模板(在 nodejs 中使用了 Babel js):
const labresult = (formData) => {
const today = new Date();
return `
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="utf-8" />
<title>LabResult</title>
<style>
body {
background: rgb(204,204,204);
}
page {
background: white;
display: block;
margin: 0 auto;
/* border: 0.5rem outset black; */
outline: 0.5rem solid black;
/* outline: 0.8rem outset black; */
outline-offset: -2rem;
/* margin-bottom: 0.5cm;
Box-shadow: 0 0 0.5cm rgba(0,0.5); */
}
page[size='A4'] {
width: 21cm;
height: 29.7cm;
}
h1,h2,h3,h4 {
font-family: 'Nunito',sans-serif;
font-weight: bold;
color: #388fe3;
}
h1 {
font-size: 2.7rem;
}
h2 {
font-size: 2.3rem;
}
h3 {
font-size: 2rem;
}
h4 {
font-size: 1.6rem;
color: #000;
}
th,td {
font-family: 'Nunito',sans-serif;
}
p {
font-family: 'Open Sans',sans-serif;
}
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 17cm;
/* Whatever width you want */
}
table {
width: 100%;
margin: 1.5rem 0;
border-collapse: collapse;
border: 1px solid #000;
}
td,th {
border: 1px solid #000;
padding: 0.5rem;
font-size: 1.15rem;
}
th {
text-align: start;
}
</style>
</head>
<body>
<page size="A4">
<h2
style="
color: rgb(221,82,47);
margin-top: 2cm;
margin-left: 5cm;
position: absolute;
text-decoration: underline;
"
>
Bahodir Shifo Diagnostika
</h2>
<p
style="
font-weight: bold;
color: rgb(40,214,98);
font-size: 2em;
margin-left: 7cm;
margin-top: 4cm;
position: absolute;
"
>
Диагностический центр
</p>
<div style="font-weight: bold; margin-top: 4.6cm; position: absolute; margin-left: 6.5cm; color: #388fe3">
<p>г. Ташкент,Яшнабадский р-н,ул. Aвиасозлар,дом 56</p>
<p style="position: relative; margin-top: -0.3cm; margin-left: 0.5cm">
+99890 983 05 72 +99890 372 54 18 (Telegram)
</p>
</div>
<div style="font-weight: bold; margin-top: 7cm; position: absolute; margin-left: 2cm; font-size: larger">
<p>Дата исследования:</p>
<p>Пациент:</p>
<p>Ден рождения:</p>
</div>
<div style="font-weight: bold; margin-top: 7cm; position: absolute; margin-left: 10cm; font-size: larger">
<p>10.11.2021</p>
<p>Jon SNow</p>
<p>11.12.1989</p>
</div>
<div style="font-weight: bold; margin-top: 11cm; position: absolute; margin-left: 2cm; font-size: larger">
<table style="width: 17cm;">
<thead>
<tr>
<th>Наименование</th>
<th>Резултат</th>
<th>Норма</th>
<th>Ед. изм.</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div style="font-weight: bold; margin-top: 23cm; position: absolute; margin-left: 2cm; font-size: larger">
<p>Интерпретация резултатов анализов толко за лечашим врачом!</p>
<p style="color: #388fe3; position: relative; margin-top: -0.5cm">"BAHODIR SHIFO" Желайет Вам здоровя</p>
<p style="color: #388fe3">В случаи утери документа копия не выдаеця</p>
<p style="font-size: larger">Врач лаборант: Дехканова Г.М_______________</p>
</div>
</page>
</body>
</html>
`;
};
export default labresult
路线如下:
import labresult from '../../utils/labresult';
router.post('/create-pdf',requireJwtAuth,(req,res) => {
create(labresult(),{}).foFile('labresult.pdf',(err) => {
console.log('API Call to create-pdf');
if (err) {
return res.status(500).json({ message: 'Failed to create pdf.' });
}
return res.status(200);
});
});
解决方法
从'../../utils/labresult'导入实验室结果;
router.post('/create-pdf',requireJwtAuth,(req,res) => {
create(labresult(),{}).toFile('labresult.pdf',(err) => {
console.log('API Call to create-pdf');
if (err) {
return res.status(500).json({ message: 'Failed to create pdf.' });
}
return res.status(200);
});
});