nkw commited on
Commit
16d3be6
·
verified ·
1 Parent(s): 205ee52

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +15 -18
index.js CHANGED
@@ -25,31 +25,21 @@ const convertMarkdownToHTML = (markdown) => {
25
  };
26
 
27
  // Fungsi untuk membuat PDF dari Markdown
28
- async function MdtoPdf(md, type) {
29
  const browser = await initBrowser;
30
  const page = await browser.newPage();
31
  const htmlContent = convertMarkdownToHTML(md);
32
  const fullHtmlContent = `
33
  <html>
34
  <head>
 
35
  <style>
36
- @font-face {
37
- font-family: 'OpenSans';
38
- src: url('https://raw.githubusercontent.com/edx/edx-fonts/refs/heads/master/open-sans/fonts/Semibold/OpenSans-Semibold.ttf') format('truetype');
39
- font-weight: 600; /* SemiBold */
40
- font-style: normal;
41
- }
42
  body {
43
- font-family: 'OpenSans', sans-serif;
44
- }
45
- .rtl {
46
- direction: rtl; /* Right-to-left for Arabic text */
47
- text-align: right; /* Align Arabic text to the right */
48
- }
49
- .ltr {
50
- direction: ltr; /* Left-to-right for normal text */
51
- text-align: left; /* Align normal text to the left */
52
  }
 
53
  </style>
54
  </head>
55
  <body>
@@ -57,10 +47,16 @@ async function MdtoPdf(md, type) {
57
  </body>
58
  </html>
59
  `;
60
- await page.setContent(fullHtmlContent, { waitUntil: "domcontentloaded" });
61
  const pdfBuffer = await page.pdf({
62
  format: type || "A4",
63
  printBackground: true,
 
 
 
 
 
 
64
  });
65
  await page.close();
66
  return pdfBuffer;
@@ -78,9 +74,10 @@ app.post("/mdtopdf", async (req, res) => {
78
  });
79
  }
80
  const md = data["md"];
 
81
  const type = data["type"] || "A4";
82
  try {
83
- const pdfBuffer = await MdtoPdf(md, type);
84
  res.set("Content-Type", "application/pdf");
85
  res.send(pdfBuffer);
86
  } catch (e) {
 
25
  };
26
 
27
  // Fungsi untuk membuat PDF dari Markdown
28
+ async function MdtoPdf(md, cssCustom, type) {
29
  const browser = await initBrowser;
30
  const page = await browser.newPage();
31
  const htmlContent = convertMarkdownToHTML(md);
32
  const fullHtmlContent = `
33
  <html>
34
  <head>
35
+ <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic&display=swap" rel="stylesheet">
36
  <style>
 
 
 
 
 
 
37
  body {
38
+ font-family: 'Noto Sans Arabic', sans-serif;
39
+ margin: 0;
40
+ padding: 0;
 
 
 
 
 
 
41
  }
42
+ ${cssCustom}
43
  </style>
44
  </head>
45
  <body>
 
47
  </body>
48
  </html>
49
  `;
50
+ await page.setContent(fullHtmlContent, { waitUntil: "networkidle0" });
51
  const pdfBuffer = await page.pdf({
52
  format: type || "A4",
53
  printBackground: true,
54
+ margin: {
55
+ top: "10mm", // Jarak atas
56
+ bottom: "10mm", // Jarak bawah
57
+ left: "10mm", // Jarak kiri
58
+ right: "10mm", // Jarak kanan
59
+ },
60
  });
61
  await page.close();
62
  return pdfBuffer;
 
74
  });
75
  }
76
  const md = data["md"];
77
+ const css = data["css"];
78
  const type = data["type"] || "A4";
79
  try {
80
+ const pdfBuffer = await MdtoPdf(md, css, type);
81
  res.set("Content-Type", "application/pdf");
82
  res.send(pdfBuffer);
83
  } catch (e) {