+ Start a Discussion
hylim1215hylim1215 

is this a good design for my VFP rendered in PDF?

Hi, i have a VFP that rendered in PDF format for my Quote. And due to the limitation whereby long text doesnt word-wrap in my table <td>, i decided to loop through my long text content and put a line break <br> after x characters.

I am doing this in my VFP exntesion class like this:
 
public class quote_ViewQuotePDFExtension {

Pattern p = Pattern.compile('[\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}\\p{IsHangul}]');
public List<QuoteLineItem> ungrp_quols {get;set;}

    public quote_ViewQuotePDFExtension(ApexPages.StandardController stdcont){
        Id quote_id = slquote.id;


        for(QuoteLineItem itm: [Select Id, UnitPrice, long_text_content__c from QuoteLineItem where QuoteId =: quote_id]){
              
             if(itm.long_text_content__c != null){
                for(Integer i=0; i< itm.long_text_content__c.length(); i++){
                  
                //loop each characters and put <br> after certain count
               // i even have regex to check if the characters is chineses, japanese or korean and increase my count + 2 else + 1

                }
               UngroupQuolItems.Rich_Line_Item_Description__c = format_str;
             }

           ungrp_quols.add(UngroupQuolItems);
         }

    }

}

of course my extesion have other logic running as well. So i would like to know if this is kind of bad design to loop each characters in my long text content to append a <br>. Will this slow down the performance like take longer to render the PDF or hit governor limits.

or it would be better to do it as trigger to append the the long text content when insert & update?

Thanks.
Best Answer chosen by hylim1215
NagendraNagendra (Salesforce Developers) 
Hi Hylim1215,

First and foremost sincerely regret for the delayed reply.

As this post is answered in the stack exchange community as below.

Generally, best to keep presentation formatting out of the stored data and only apply formatting when needed as you are doing.

I'd be a little cautious about stepping through each character especially if the text can be thousands of characters long. (More to avoid sluggish performance that the 10s CPU limit.) This sort of loop to split the text up would be less expensive:
Integer size = 10;
String text = 'abcdefghijklmnopqrstuvwxyz';

String[] parts = new String[] {};
for (Integer start = 0; start < text.length(); start += size) {
    parts.add(text.substring(start, Math.min(text.length(), start + size)));
}
assuming you can do your Chinese/Japanese/Korean determination up front.

In controller extension form:
public with sharing class quote_ViewQuotePDFExtension {

    private static final Integer MAX = 100;

    private ApexPages.StandardController sc;

    public quote_ViewQuotePDFExtension(ApexPages.StandardController sc) {
        this.sc = sc;
    }
    public String [] getLongTextContentParts() {
        String[] parts = new String[] {};
        for (QuoteLineItem item : [
                Select long_text_content__c
                from QuoteLineItem
                where QuoteId = :sc.getId()
                ]) {
            String text = item.long_text_content__c;
            if (!String.isEmpty(text)) {
                for (Integer start = 0; start < text.length(); start += MAX) {
                    parts.add(text.substring(start, Math.min(text.length(), start + MAX)));
                }
            }
        }
        return parts;
    }
}



You'd get far better performance using replaceAll:
UngroupQuolItems.Rich_Line_Item_Description__c =
   itm.long_text_content__c.replaceAll('(.{100})','$1<br/>');
No loops, no substrings, just pure speed. Adjust 100 to whatever number you feel is appropriate. More complex patterns are also possible, it really depends on your exact needs.

Kindly mark this post as solved so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.

Best Regards,
Nagendra.P