Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direction RTL #833

Open
AbdelrhmanMohamed opened this issue Oct 19, 2021 · 4 comments
Open

Direction RTL #833

AbdelrhmanMohamed opened this issue Oct 19, 2021 · 4 comments

Comments

@AbdelrhmanMohamed
Copy link

jsPDF with autotable plugin table does not obey right to left text direction from html element attribute values such as

https://stackoverflow.com/questions/59843249/jspdf-with-autotable-plugin-table-does-not-obey-right-to-left-text-direction-fro

@simonbengtsson
Copy link
Owner

Would be great to support indeed. Let me know if you are interesting in adding support for this.

@lexabug
Copy link

lexabug commented Feb 21, 2023

Yes, would be great to have this support.

@ZaidSameer
Copy link

Any Updates?

@mahyabagheri
Copy link

mahyabagheri commented Sep 13, 2024

Hello , I've just found a solution for RTL direction in jsPDF-AutoTable,
Here in autoTableText.ts file in the source code add this config {isOutputRtl: true} if the string contains your rtl language alphabet, here I check for persian alphabet and also change parentheses:

if (styles.halign === 'center' || styles.halign === 'right') {
    let alignSize = fontSize
    if (styles.halign === 'center') alignSize *= 0.5

    if (splitText && lineCount >= 1) {
      for (let iLine = 0; iLine < splitText.length; iLine++) {
       if(checkIfContainsPersianAlphabet){
          const rtlText = checkForParentheses(splitText[iLine]);
          doc.text(
            rtlText ,
            x - doc.getStringUnitWidth(splitText[iLine]) * alignSize,
            y, {isOutputRtl: true}
          )
        }else {
          doc.text(
            splitText[iLine],
            x - doc.getStringUnitWidth(splitText[iLine]) * alignSize,
            y,
          )
        }
        y += lineHeight
      }
      return doc
    }
    x -= doc.getStringUnitWidth(text) * alignSize
  }

  if (styles.halign === 'justify') {
    doc.text(text, x, y, {
      maxWidth: styles.maxWidth || 100,
      align: 'justify',
    })
  } else {
    doc.text(text, x, y)
  }

  return doc
}

function checkIfContainsPersianAlphabet(str: string): boolean {
    const persianAlphabetRegex = /[\u0600-\u06FF\u067E\u0686\u0698\u06A9]/;
    return persianAlphabetRegex .test(str);
  }
  function checkForParentheses(str: string): boolean {
    const rtlTextArray = [];
    str.split("").forEach(ch => {
        if (ch === "(") {
          rtlTextArray.push(")")
        } else if (ch === ")") {
          rtlTextArray.push("(")
        } else {
          rtlTextArray.push(ch)
        }

      }
    )
    const rtlText = rtlTextArray.join("");
    return rtlText
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants