我正在使用Node.js 应用程序的100包来生成PDF,但当我try 生成阿拉伯文本时遇到了一个问题.它总是颠倒阿拉伯文本中的数字.

如果我想写:UNICODE 1992(1992年),它总是更改为(ساڵی),即使当我使用UNICODE自定义字体时,这个问题仍然存在.

这是一个图像中的示例:

This is an example in image

以下是我的代码:

function generatePDFForRuleAndRegulation(data) {
  const fileNamePDF = `${data.name}.pdf`;
  const doc = new PDFDocument();

  // Pipe the PDF document to a writable stream (file in this case)
  const stream = fs.createWriteStream(fileNamePDF);

  doc.pipe(stream);

  doc
    .font(`${path.join(__dirname, "../../assets/fonts/NRT-Reg.ttf")}`)
    .fontSize(12)
    .text(`${data.name}`, {
      align: "center",
    });
  doc.moveDown(); // Move cursor down

  doc
    .font(`${path.join(__dirname, "../../assets/fonts/Rabar_021.ttf")}`)
    .fontSize(10)
    .text(data.text, { align: "right", features: ["rtla"] });

  // Finalize the PDF document
  doc.end();

  // Inform when the file has been created
  stream.on("finish", () => {
    console.log(`PDF created: ${fileNamePDF}`);
  });
}

您有什么建议的解决方案或变通办法吗?或者,您是否知道有其他程序包可以生成也支持阿拉伯字体的PDF?

推荐答案

最后,我使用了定制的RTL,而不是100插件.

// Function to check if text contains Hebrew characters
const isHebrew = (text) => {
  return text.search(/[\u0590-\u05FF]/) >= 0;
};

// Function to check if text contains Arabic characters
const isArabic = (text) => {
  return text.search(/[\u0600-\u06FF]/) >= 0;
};

// Function to reverse the order of words if the text is in Hebrew or Arabic
const rightToLeftText = (text) => {
  if (isHebrew(text) || isArabic(text)) {
    return text.split(" ").reverse().join(" ");
  } else {
    return text;
  }
};

// Example usage:
// Apply the rightToLeftText function to the "data.text" and store the result in the "text" variable
const text = rightToLeftText(data.text);

// Use a PDF generation library (assumed to be "doc") to set font, size, and text alignment
doc
  .font(`${path.join(__dirname, "../../assets/fonts/Rabar_021.ttf")}`)
  .fontSize(10)
  .text(text, { align: "right" });

Javascript相关问答推荐

用相器进行向内碰撞检测

序列查找器功能应用默认值而不是读取响应

获取表格的左滚动位置

使用useup时,React-Redux无法找到Redux上下文值

有条件的悲剧

如何避免移动设备中出现虚假调整大小事件?

Bootstrap动态选项卡在切换选项卡后保持活动状态,导致元素堆叠

如何在Angular中插入动态组件

在这种情况下,如何 for each 元素添加id?

在网页上添加谷歌亵渎词

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

Web Crypto API解密失败,RSA-OAEP

将基元传递给THEN处理程序

在ChartJS中使用spanGaps时,是否获取空值的坐标?

将延迟加载的模块转换为Eager 加载的模块

如何在Firebase中读取对象的特定字段?

使用python,我如何判断一个html复选框是否被隐藏,以及它是否被S选中?

如何从Reaction-Redux中来自API调用的数据中筛选值

JS/css:将数字输入的S函数和可调整大小的元素S函数绑定在一起

如何在不获取其子元素的文本内容的情况下获取元素的文本内容?