String to pdf

String to pdf

Forum member · Apr 3, 2007
Hi i've been tampering with your product and planning to buy it. what i'm trying to do is convert a string containing html code(it's a string that changes all the time so i can't use a file) to an online pdf. I used the formattedtextcontinuation servlet as base code because sometimes the text takes more than 1 page but i want it to be 1 column only and i can't seem to get it right. also i'd like to open the pdf in a new page and not the current page cause otherwise the users can't continue on my website without using the back button. any pointers thx in advance sOut = response.getOutputStream(); template = new Template(); // Add the page numbering label to the page template objPageNumLabel = new PageNumberingLabel("Page %%CP%% of %%TP%%", 0,494, 684, 10, Font.getHelvetica(),10, TextAlign.CENTER); template.getElements().add(objPageNumLabel); /*try { Image objImage = new Image(getServletContext().getRealPath("images"+"/DPDFLogo.png"), 342, 0, 0.18f); objImage.setAlign(Align.CENTER); template.getElements().add(objImage); } catch (FileNotFoundException ex) { System.err.println("cannot load the image :"+ex); }*/ String strFormattedText = tekst;(tekst is holding the html formatted text) // Create a document and set it's properties Document objDocument = new Document(); objDocument.setCreator("HTMLTextContinuation.java"); objDocument.setAuthor("Your Name"); objDocument.setTitle("Formatted Text Continuation"); // Add the template to the document objDocument.setTemplate(template); // Create an FormattedTextAreaStyle and set the initial properties for the // FormattedTextArea FormattedTextAreaStyle objStyle = new FormattedTextAreaStyle(FontFamily .getHelvetica(), 10, false); objStyle.paragraph.indent = 36; objStyle.paragraph.spacingAfter = 6; objStyle.paragraph.allowOrphanLines = false; // Create an FormattedTextArea using the style FormattedTextArea objFormattedTextArea = new FormattedTextArea(strFormattedText, 0, 60, 315, 424, objStyle); // Loop untill all of the text is displayed while (objFormattedTextArea != null) { objFormattedTextArea = addTextToDocument(objDocument, objFormattedTextArea); } // Outputs the document to the current web page. objDocument.drawToWeb(request, response, sOut, "FormattedTextContinuation.pdf"); sOut.close(); } private FormattedTextArea addTextToDocument(Document document, FormattedTextArea formattedTextArea) { // Create a new page Page objPage = new Page(PageSize.LETTER, PageOrientation.PORTRAIT, 54.0f); // Add left column FormattedTextArea to page objPage.getElements().add(formattedTextArea); // Create the right column FormattedTextArea (How do i just add all of this to 1 column spread over the entire page, i'm printing letters and want to keep my html layout) formattedTextArea = formattedTextArea.getOverflowFormattedTextArea(369, 60); // Add it to the page if it had overflow text if (formattedTextArea != null) { objPage.getElements().add(formattedTextArea); } // Add page to document document.getPages().add(objPage); // Return the next FormattedTextArea if there is overflow text if (formattedTextArea == null) { return null; } else { return formattedTextArea.getOverflowFormattedTextArea(0, 60); } }

Re: String to pdf

Support · Apr 3, 2007
Hello, Yes, you can add formatted text in a single column in the page without any problem. In the FormattedTextContinuation example the text is added side by side in a single page by changing the X, and Y coordinates of the formatted text area. You can change these X, Y properties according to your requirement. You can refer to the example Formated text area on our web site. You can open the PDF in a new window by specifying the target attribute of the form tag to _blank. It will open the PDF in a new window. Thanks, ceTe Software Support Team.

Re: String to pdf

Forum member · Apr 3, 2007
it works now but how do i let the text fill the entire page cause now it spreads the amount of 2 pages over 12 pages. sOut = response.getOutputStream(); // Create a PDF Document Document document = new Document(); String variableText = tekst; // Create an Formatted text area FormattedTextArea formattedTextArea = new FormattedTextArea(variableText, 0, 0, 250, 200, FontFamily.getHelvetica(), 12, false); do { // Create a new page Page page = new Page(); // Add the text area to the page; page.getElements().add( formattedTextArea ); // Add the page to the document document.getPages().add( page ); // Set the Formatted text area object equal to the rest of the text that did not fit // if all the text did fit, GetOverflowFormattedTextArea will return null formattedTextArea = formattedTextArea.getOverflowFormattedTextArea(); } while( formattedTextArea != null ); // Save the PDF document.drawToWeb(request, response, sOut,"CPG.pdf"); }

Re: String to pdf

Support · Apr 3, 2007
Hello, In your code you are specifying the height and width of the formatted text area as 250, and 200 in the constructor. This is the reason for splitting in more number of pages as this size is small. You will have to increase these height and width to sufficient dimensions so that the formatted text area fits in the page. Thanks, ceTe Software Support Team.

Re: String to pdf

Forum member · Apr 4, 2007
yes i know but how do i let the text that can't be shown on page 1 get autotransferred to the enxt page and so on for the following pages.

Re: String to pdf

Support · Apr 4, 2007
Hello, The text that can not be fitted on a single page for the given dimensions of the formatted text area can be captured by using the getOverflowFormattedTextArea method of the formatted text area. You can add this over flow text to a new page. You can do this in a loop umtill the over flow text exists. You can use the above code for this by changing the formatted text area's height and width. Thanks, ceTe Software Support Team.

Re: String to pdf

Forum member · Apr 4, 2007
i'm sorry but i have 1 more question :) is it possible to use <pre> tags in my html code cause i can't seem to find a way to keep my whitespacing from my html code working inside the pdf document. or are there any other tags i can use to do spaces and tabs?

Re: String to pdf

Support · Apr 4, 2007
Hello, You can preserve the white spaces in your html text by setting the preserveWhitespace parameter to true in the FormattedTextArea's constructor. You can also use the FormattedTextAreaStyle class which takes the preserveWhitespace as boolean in its constructor. Thanks, ceTe Software Support Team.

Re: String to pdf

Forum member · Apr 4, 2007
i don't understand it. i've set it to true there's alot of white space in the line but everywhere he cuts the white space to only a few characters

Re: String to pdf

Support · Apr 4, 2007
Hello, What version and build of our product are you using? Please make sure that you are using the latest build of that version. Following code is working fine for us, we are able to preserve the spaces by specifying true. Please try this code and see if you have problems. Document document = new Document(); FormattedTextArea area = new FormattedTextArea("ceTe Software Support Team",10,10,500,500,FontFamily.getHelvetica(),15,true); Page page = new Page(); page.getElements().add(area); document.getPages().add(page); document.draw("C:/Temp/output.pdf"); Thanks, ceTe Software Support Team.

Re: String to pdf

Forum member · Apr 4, 2007
build 4.0 String variableText = tekst; FormattedTextArea formattedTextArea = new FormattedTextArea(variableText,10,10,500,500,FontFamily.getHelvetica(),12,true); do { Page page = new Page(); page.getElements().add( formattedTextArea ); document.getPages().add( page ); formattedTextArea = formattedTextArea.getOverflowFormattedTextArea(0,0); } while( formattedTextArea != null ); document.drawToWeb(request, response, sOut,"CPG.pdf");

Re: String to pdf

Support · Apr 4, 2007
Hello, Can you please send over the code you are using and the out put PDF document to our [^a|mailto:support@cete.com|Support Team^] so that they can take a look at it. Also send us the text you are passing in the variableText variable. Thanks, ceTe Software Support Team.