Align (Justify) Arabic text in jsPDF

Viewed 169

I am using npm jspdf 2.3.1 in my react application.

Every thing is working fine. I want to justify the text but the starting point should be "right".

const [state, setState] = useState({
doc: new jsPDF(),
});

const myPdf = ( ) => {
state.doc.addFileToVFS("MyFont.ttf", myFont); // Arabic Font
state.doc.addFont("MyFont.ttf", "MyFont", "normal");

 state.doc.text(
  "لوريم إيبسوم هو ببساطة نص شكلي (بمعنى أن الغاية هي الشكل وليس",
  190,
  10,
  "right"
);

  state.doc.save();
}

What I tried was like this

state.doc.text(
    "لوريم إيبسوم هو ببساطة نص شكلي (بمعنى أن الغاية هي الشكل وليس",
    190, //it prints left to right
    10,
    { maxWidth: 250, align: "justify" } //it's not justifying the text
);

Am I doing it wrong or do I need to use a plugin? Can I keep both "right" and "justify", if yes, how?

1 Answers

When using the 'right' alignment, the text will be displayed to left of the x coordinate.

Thus, you will need to adjust your coordinates accordingly. One such way is to get the maximum width of the page and then use it as the basis for everything you need in your page right-to-left.

let pageWidth = doc.internal.pageSize.width || doc.internal.pageSize.getWidth();
let distanceFromRight = 10

doc.text(
    "لوريم إيبسوم هو ببساطة نص شكلي (بمعنى أن الغاية هي الشكل وليس",
    pageWidth - distanceFromRight ,
    20,
    "right"
);
Related