You need to sign in to do that
Don't have an account?
Marco Zeuli
How to wrap long URL when rendering page as PDF
Hi guys,
i want to share with you a solution I come up with, to wrap long url when rendering a VF page.
Basically the problem was this:
A customer pasted a very long URL (more than 400 chars) in a Rich Text Area. When rendering the page as PDF, to print it, the URL overflow the page margin, so it wasn't visible at all.
This is the code i come up with
You could use like this:
i want to share with you a solution I come up with, to wrap long url when rendering a VF page.
Basically the problem was this:
A customer pasted a very long URL (more than 400 chars) in a Rich Text Area. When rendering the page as PDF, to print it, the URL overflow the page margin, so it wasn't visible at all.
This is the code i come up with
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; }
You could use like this:
MyCustomObject.richText = addWhiteSpaceInUrlTooLOng(MyCustomObject.richText);Hope it will be useful ;)
But had a same problem for a long text field. And i wrote almost similar code to over come this. thought it would be useful for others so sharing it.