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



我已经在整个互联网上搜索了几个小时,我需要一个支持希伯来语的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;

等待您的帮助!非常感谢。

我修复了代码,以一种间接的方式,我将样式添加到文本和autoTable:

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("דוח שעות:");

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

2023年8月

我一开始和你一样,但最终发现解决方案更简单。也许当你问这个问题的时候,它还不存在,但为了那些在谷歌上遇到这个问题的人的利益,它应该在这里。应该使用jsPDF的setR2L函数。

const doc = new jsPDF();
doc.addFont("David-normal.ttf", "David", "normal");
doc.setR2L(true);
// doc.autoTable(tableColumn... // the rest

为了避免翻转英语单词(例如数字(的问题,我使用了你的函数来达到相反的目的:

function fixEn(str) {
if (/[א-ת]/.test(String(str))) return str;
return String(str).split("").reverse().join("");
}

相关内容

最新更新