vendredi 30 octobre 2020

How to display Thai text with proper word wrap in PDF?

I'm using Laravel Framework to develop a pdf generated by using variables from Controller and display in THAI language. The problem is whenever THAI language is displayed and justified. It's not in the correct form. For example, the string "พัฒนาสำเร็จเทคโนโลยีผลักดันนานาชาติทรัพยากรส่งเสริมเอสเอ็มอีระดับสากลจัดการคุณธรรมโทรคมนาคมวิสาหกิจบริหารสตาร์ทอัพสร้างสรรค์จิตวิญญาณสนับสนุนลงทุนโลกาภิวัฒน์สตาร์ทอัพผลักดันยุคใหม่ลงทุนเอสเอ็มอีจริยธรรมนวัตกรรมผสมผสานเป้าหมายก้าวหน้าสำเร็จล้ำสมัยคุณธรรมพลเมืองเทคโนโลยีโลกาภิวัฒน์ความสัมพันธ์บูรณาการสร้างสรรค์บริหารพันธกิจสร้างสรรค์พลังงานเทคโนโลยีบริหารเอสเอ็มอีสตาร์ทอัพคุณธรรมสำเร็จจัดการจิตวิญญาณยุคใหม่มุ่งมั่นวิสาหกิจวาทกรรมสุขภาวะสนับสนุนต่างประเทศกระแสเป้าหมายอาเซียนกระแสวิสาหกิจสนธิกำลังสุขภาวะเป้าหมายบูรณาการเทคโนโลยีเทคโนโลยีผสมผสานพลังงานบริหารเป้าหมายผลักดันโครงสร้างระดับสากลพันธกิจวาทกรรมทรัพยากรพัฒนาระดับสากลทรัพยากรจิตวิญญาณกระแสจัดการส่งเสริมต่างประเทศสตาร์ทอัพคุณธรรมสุขภาวะเทคโนโลยีวาทกรรมโครงสร้างประชารัฐโลกาภิวัฒน์บริหารผสมผสานนานาชาติยุคใหม่ความสัมพันธ์อาเซียนโลกาภิวัฒน์บูรณาการสนธิกำลังจริยธรรมวาทกรรมจัดการผสมผสานล้ำสมัยมุ่งมั่นผลักดันพลเมืองนานาชาติพลังงานโครงสร้างบริหารจิตวิญญาณส่งเสริมเทคโนโลยีพันธกิจ" will be displayed in this form >>

Example of Incorrect Thai Text Display

The Javascript I used as below >>

function widthCharCount(txt){
if(!txt) return 0;
  var thaiFullWidthCharRegex = /[^\u0E31\u0E34-\u0E3E\u0E47-\u0E4E]/g;
  return txt.match(thaiFullWidthCharRegex).length;
}
function wrapThaiText(segThaiTxt,maxLength,linebreak){
  linebreak = linebreak || '\n';
  //thai word segmentation with '|'
  var words = segThaiTxt.split('|');
  var txt = '';
  //loop from start words
  for (var i = 0, line = '', linewlength = 0; i < words.length; i++){
    var wlen = widthCharCount(words[i]);
    if(linewlength + wlen <= maxLength){
      line = line + words[i];
      linewlength +=  wlen;
      
    } else { //word exceed line length
      //add line to txt
      txt = txt + (line+linebreak);
      //move the word to new line
      line = words[i];
      linewlength = wlen;
    }
  }
  if( linewlength > 0 ) {
    txt = txt + line;
  }
  return txt;
}
function wrapText() {
  var inlen = $("#in-len").val();
  //replace new line with ^ before send to word segment
  var thaiTxt = $("#in-txt").val().replace(/\n/g,'^');
  $.ajax( "https://s2.adsstat.com/swath",
         { crossDomain: true,
          data: thaiTxt,
          contentType: 'text/plain; charset=UTF-8',
          type: 'POST',
          success:function( data ) {//replace work segmentation new line with <wbr/>, <br/>
                    $( "#out-txt-swath" ).html( wrapThaiText(data.replace(/\^/g,'\n'),inlen,'<br />'));
                  }
         });
}
$("#in-txt").on( "change", wrapText );
$("#in-len").on( "change", wrapText );

When I hard code the THAI text directly in the div like this >>

<input id="in-len" type="text" value="50" style="display: none;"/>
<div id="in-txt">ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนาใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา ใดใดในโลกล้วน อนิจจา สิ้นซึ่งสังขารลา เที่ยงแท้ คงแต่บาปบุญมาพาส่ง สู่ที่โปรดนา</div>
<div id="out-txt-swath"></div>

When I'm using the code like above. The text is displayed and break lines correctly.

I don't know why hard code text and text from or php tag are differ from each other in some way. Any ideas for this? If you got an idea, please let me know. Thank you in advance.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3oG1IHq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire