Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -23,18 +23,24 @@ app.get("/", (_req, res) => {
|
|
| 23 |
method: "POST",
|
| 24 |
url: "/api/html-to-pdf",
|
| 25 |
body: {
|
| 26 |
-
html_code: "Your HTML content here",
|
| 27 |
pdf_options: {
|
| 28 |
format: "A4",
|
| 29 |
printBackground: true,
|
| 30 |
margin: {
|
| 31 |
-
top: "
|
| 32 |
-
right: "
|
| 33 |
bottom: "0mm",
|
| 34 |
-
left: "
|
| 35 |
}
|
| 36 |
}
|
| 37 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
});
|
| 40 |
});
|
|
@@ -88,34 +94,71 @@ app.post("/api/html-to-pdf", async (req, res) => {
|
|
| 88 |
|
| 89 |
const page = await browser.newPage();
|
| 90 |
|
| 91 |
-
// Set content
|
| 92 |
await page.setContent(
|
| 93 |
`<!DOCTYPE html>
|
| 94 |
<html>
|
| 95 |
<head>
|
| 96 |
<meta charset="utf-8" />
|
| 97 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
</head>
|
| 99 |
<body>
|
| 100 |
${htmlContent}
|
| 101 |
</body>
|
| 102 |
</html>`,
|
| 103 |
{
|
| 104 |
-
waitUntil: "networkidle0",
|
| 105 |
timeout: 30000
|
| 106 |
}
|
| 107 |
);
|
| 108 |
|
| 109 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
const defaultPdfOptions = {
|
| 111 |
format: "A4",
|
| 112 |
printBackground: true,
|
| 113 |
margin: {
|
| 114 |
-
top: "
|
| 115 |
-
right: "
|
| 116 |
bottom: "0mm",
|
| 117 |
-
left: "
|
| 118 |
-
}
|
|
|
|
| 119 |
};
|
| 120 |
|
| 121 |
const pdfBuffer = await page.pdf({
|
|
|
|
| 23 |
method: "POST",
|
| 24 |
url: "/api/html-to-pdf",
|
| 25 |
body: {
|
| 26 |
+
html_code: "Your HTML content here (with inline styles and complete structure)",
|
| 27 |
pdf_options: {
|
| 28 |
format: "A4",
|
| 29 |
printBackground: true,
|
| 30 |
margin: {
|
| 31 |
+
top: "0mm",
|
| 32 |
+
right: "0mm",
|
| 33 |
bottom: "0mm",
|
| 34 |
+
left: "0mm"
|
| 35 |
}
|
| 36 |
}
|
| 37 |
+
},
|
| 38 |
+
notes: [
|
| 39 |
+
"HTML should include all styles inline or in <style> tags",
|
| 40 |
+
"Use @page CSS rules for page-specific styling",
|
| 41 |
+
"Margins default to 0mm - add padding in your HTML instead",
|
| 42 |
+
"Charts/images will be rendered - allow 1.5s load time"
|
| 43 |
+
]
|
| 44 |
}
|
| 45 |
});
|
| 46 |
});
|
|
|
|
| 94 |
|
| 95 |
const page = await browser.newPage();
|
| 96 |
|
| 97 |
+
// Set content with PDF-optimized styling
|
| 98 |
await page.setContent(
|
| 99 |
`<!DOCTYPE html>
|
| 100 |
<html>
|
| 101 |
<head>
|
| 102 |
<meta charset="utf-8" />
|
| 103 |
+
<style>
|
| 104 |
+
@page {
|
| 105 |
+
margin: 0;
|
| 106 |
+
size: A4;
|
| 107 |
+
}
|
| 108 |
+
* {
|
| 109 |
+
margin: 0;
|
| 110 |
+
padding: 0;
|
| 111 |
+
box-sizing: border-box;
|
| 112 |
+
}
|
| 113 |
+
html, body {
|
| 114 |
+
width: 100%;
|
| 115 |
+
height: 100%;
|
| 116 |
+
margin: 0;
|
| 117 |
+
padding: 0;
|
| 118 |
+
}
|
| 119 |
+
body {
|
| 120 |
+
-webkit-print-color-adjust: exact;
|
| 121 |
+
print-color-adjust: exact;
|
| 122 |
+
color-adjust: exact;
|
| 123 |
+
}
|
| 124 |
+
img, svg, canvas {
|
| 125 |
+
max-width: 100%;
|
| 126 |
+
height: auto;
|
| 127 |
+
display: block;
|
| 128 |
+
}
|
| 129 |
+
table {
|
| 130 |
+
border-collapse: collapse;
|
| 131 |
+
width: 100%;
|
| 132 |
+
}
|
| 133 |
+
</style>
|
| 134 |
</head>
|
| 135 |
<body>
|
| 136 |
${htmlContent}
|
| 137 |
</body>
|
| 138 |
</html>`,
|
| 139 |
{
|
| 140 |
+
waitUntil: ["load", "domcontentloaded", "networkidle0"],
|
| 141 |
timeout: 30000
|
| 142 |
}
|
| 143 |
);
|
| 144 |
|
| 145 |
+
// Wait for fonts and images to load
|
| 146 |
+
await page.evaluateHandle('document.fonts.ready');
|
| 147 |
+
|
| 148 |
+
// Wait for any dynamic content (charts, images, etc.)
|
| 149 |
+
await page.waitForTimeout(1500);
|
| 150 |
+
|
| 151 |
+
// Generate PDF with optimized options
|
| 152 |
const defaultPdfOptions = {
|
| 153 |
format: "A4",
|
| 154 |
printBackground: true,
|
| 155 |
margin: {
|
| 156 |
+
top: "0mm",
|
| 157 |
+
right: "0mm",
|
| 158 |
bottom: "0mm",
|
| 159 |
+
left: "0mm"
|
| 160 |
+
},
|
| 161 |
+
preferCSSPageSize: true
|
| 162 |
};
|
| 163 |
|
| 164 |
const pdfBuffer = await page.pdf({
|