2

I have created a Javascript button, When user clicks on Javascript button it generates a PDF document. I have used renderAs Pdf attribute to generate pdf document from VF page. My problem is I have a rich text area field and i am displaying it in PDF. It is displaying correctly if rich text area does not have any links inside it.

But if there is link in the filed it overlapping TD tag.

I tried to use following CSS, It is working for VF page but it is not working for PDF page.

td {
   word-break: break-all;
   word-break: break-word;
}

enter image description here

Thanks for your help in Advance.

  • We had similar issue in my previous project..If we have a long url, salesforce does not break the url..Here is link from salesforce..help.salesforce.com/… – javanoob Aug 26 '15 at 2:46
  • 1
    looks like there is no solution for this problem...Here is similar question but no answer : salesforce.stackexchange.com/questions/45422/… – javanoob Aug 26 '15 at 2:54
  • @Javanoob thanks for the quick response. You are right... I googled it but did not get relevance answer. – MIX DML Aug 26 '15 at 3:00
3

Here is a similar solution posted in dev forum. Hope this helps. I have copied the solution here.

private String addWhiteSpaceInUrlTooLong(String text) {
        // Step 1 - Search anchor links
        Pattern ptn = Pattern.compile('<a[^>]*(>.*?)</a>'); // WATCH OUT! This regex doesn't match nested anchor
        Matcher mch = ptn.matcher(text);
        Integer charPerLine = 50; // A whitespace is inserted each charPerLine chars
        while (mch.find()) {
            String toReplace = mch.group(1);
            String substitute = '';
            Integer len = toReplace.length();

            if (len < charPerLine) //No need to replace
                continue;

            Integer elems; // White space to insert

            if (len / charPerLine == 0)
                elems = len / charPerLine;
            else
                elems = len / charPerLine + 1;

            // Insert white spaces
            for (Integer i = 1; i <= elems; i++) {
                if ((charPerLine * i) < len)
                    substitute += toReplace.substring(charPerLine * (i - 1), charPerLine * i) + ' ';
                else
                    substitute += toReplace.substring(charPerLine * (i - 1), len) + ' ';
            }

            text = text.replace(toReplace, substitute);            
        }

        // Step 2 - Search pasted links
        ptn = Pattern.compile('\\b\\s(https?://\\S.*?)(\\s|$)');
        mch = ptn.matcher(text);
        charPerLine = 60;

        while(mch.find()) {
            String toReplace = mch.group();
            String substitute = '';
            Integer len = toReplace.length();

            if (len < charPerLine)
                continue;

            Integer elems;

            if (len / charPerLine == 0)
                elems = len / charPerLine;
            else
                elems = len / charPerLine + 1;

            // Insert white spaces
            for (Integer i = 1; i <= elems; i++) {
                if ((charPerLine * i) < len)
                    substitute += toReplace.substring(charPerLine * (i - 1), charPerLine * i) + ' ';
                else
                    substitute += toReplace.substring(charPerLine * (i - 1), len) + ' ';
            }

            text = text.replace(toReplace, substitute);
        }

        return text;
    }

Use this method as below.

MyCustomObject.richText = addWhiteSpaceInUrlTooLOng(MyCustomObject.richText);
  • Thanks @Saroj I try above code and will get back to you with feedback. – MIX DML Aug 26 '15 at 4:18
0

I had a same problem with URL in pdf but it was a long text field. The word wrap was not working for the URL as there are no white spaces in it. The URL was not wrapping and it was going beyond the page. i used the below code to add white spaces to over come this issue.

public static string insertBreak(String hyperLink) { // hyperLink is the URL
        final Integer linkLength = hyperLink.length();
        final Integer maxLength = 100;
        Integer varCount = (linkLength/maxLength);

        if (math.mod(linkLength, maxLength) > 0)
            varCount++;

        String fullLink = '';
        integer startValue = 0;
        integer endValue = maxLength;
        for (Integer i = varCount; i >= 0; i--) { 
            fullLink += hyperLink.substring(startValue, endValue) + ' ';
            startValue = endValue;
            endValue = (endValue + maxLength) > linkLength ? linkLength : (endValue + maxLength);
        }
        return fullLink;
    }

While rendering the page i will display the URL text with white spaces only when the page is rendered as PDF. otherwise i will display the normal URL text without white spaces.

Hope this helps.... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.