如何在React中生成包含表格的希伯来语PDF?

问题描述 投票:0回答:2

我已经在整个互联网上搜索了几个小时,我需要一个支持希伯来语的 PDF 生成器!我也需要在 PDF 中有一个表格。 我尝试过 JSpdf,但我得到的文本垃圾是翻转的,而不是翻转的。 我使用 JSpdf 的 autoTable 来制作我的表格,但我没有设法在表格中包含希伯来语,甚至没有翻转(我得到了垃圾)。

我愿意接受任何建议,不仅仅是 JSpdf,我需要任何有用的东西。

这是我的代码:

David-normal.js:

import { jsPDF } from "jspdf"
var font = '<here comes a very long string of the fone>';
var callAddFont = function () {
  this.addFileToVFS('David-normal.ttf', font);
  this.addFont('David-normal.ttf', 'David', 'normal');
};
jsPDF.API.events.push(['addFonts', callAddFont])

reportGenerator.js:

import jsPDF from "jspdf";
import "jspdf-autotable";
import "../components/David-normal.js";


// Date Fns is used to format the dates we receive
// from our API call
import { format } from "date-fns";

// define a generatePDF function that accepts a reports argument
const generatePDF = reports => {
  //initialize jsPDF
  const doc = new jsPDF();
  doc.addFont("David-normal.ttf", "David", "normal");
   doc.setFont("David"); // set font
   doc.setFontSize(12);




  // define the columns we want and their titles
  const tableColumn = [ "שם עובד", "מספר פרויקט", "תאריך דיוח", "כניסה", "יציאה", "תיאור"];
  // define an empty array of rows
  const tableRows = [];

  // for each report pass all its data into an array
  reports.forEach(report => {
    const reportData = [
      report.worker_id,
      report.project_id,
      report.reporting_date,
      report.start_time,
      report.end_time,
      report.description,
      // called date-fns to format the date on the report
      format(new Date(report.reporting_date), "yyyy-MM-dd")
    ];
    // push each reports's info into a row
    tableRows.push(reportData);
  });
  // startY is basically margin-top

  doc.autoTable(tableColumn, tableRows, { font: 'David', align: 'right', isSymmetricSwapping: true, isInputVisual: true, isOutputVisual: false });
  const date = Date().split(" ");
  // we use a date string to generate our filename.
  const dateStr = date[0] + date[1] + date[2] + date[3] + date[4];
  // reports title.
  let text = "דוח שעות:"
  doc.text(text, 100, 10, {align: 'right', isSymmetricSwapping: true, isInputVisual: true, isOutputVisual: false});
  // we define the name of our PDF file.
  doc.save(`report_${dateStr}.pdf`);
};

export default generatePDF;

等待您的帮助!谢谢!

reactjs jspdf hebrew jspdf-autotable
2个回答
0
投票

我修复了代码,以间接的方式,将样式添加到文本和自动表中:

  doc.autoTable(tableColumn, tableRows, {styles: {font: "David", align: 'right', isSymmetricSwapping: true, isInputVisual: true, isOutputVisual: false}});
doc.text(text, 100, 10, {styles: {font: "David" }});

并制作了一个反向函数来修复字母:

function fix(str){
   return str.split('').reverse().join('');
}

然后,只要我知道有希伯来语,我就调用它,例如:

let text = fix("דוח שעות:");

不幸的是,我的解决方案也翻转了英文单词。


0
投票

你是怎么做到的? 我尝试使用字体 t= 的转换器,但它不起作用

© www.soinside.com 2019 - 2024. All rights reserved.