Forum How do I...?

using prince in java

PotentialCustomer
I am trying to use prince in a java app prior to deciding if we want to buy a license. I have put together the following code adn have some questions.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.princexml.Prince;

public class someprincetest {
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// location of prince command line
		Prince p = new Prince("C:\\foo\\prince.exe");
		boolean result;
		InputStream is = null;
		try {

			result = p.convert("C:\\foo\\bar\\doc.html");
			if (result)
				System.out.println("String File Worked");
			else
				System.out.println("String File Failed");

			// location of my html file
			File file = new File("C:\\foo\\bar\\doc2.html");
			is = new FileInputStream(file);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			result = p.convert(is, baos);
			if (result)
				System.out.println("FileInputStream Worked");
			else
				System.out.println("FileInputStream Failed");

			String html = "<html><head></head><body>hello world</body></html>";
			ByteArrayInputStream bs = new ByteArrayInputStream(html.getBytes("UTF-8"));
			result = p.convert(bs, baos);
			if (result)
				System.out.println("ByteArrayInputStream Worked");
			else
				System.out.println("ByteArrayInputStream Failed");

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				is.close();
			}
		}
	}
}


first convert works, generates a pdf, and prints out appropriate message to console. second convert(file), doesn't generate a pdf, and returns fail. third convert prints works to console but doesn't create pdf.

What change should I make to make second convert work? What change do i need to make to third convert to make it generate a pdf? Thanks
mikeday
Can you try calling setLog() before convert() to specify a log file? Then we can see if Prince is generating any error or warning messages.
PotentialCustomer
mikeday wrote:
Can you try calling setLog() before convert() to specify a log file? Then we can see if Prince is generating any error or warning messages.



Wed Jun 29 09:09:38 2011: ---- begin
Wed Jun 29 09:09:38 2011: Loading document...
Wed Jun 29 09:09:38 2011: file:///C:/foo/bar/doc.html:11: error: Unexpected end tag : br
Wed Jun 29 09:09:38 2011: file:///C:/foo/bar/doc.html:60: error: Tag nobr invalid
Wed Jun 29 09:09:38 2011: file:///C:/foo/bar/doc.html:60: error: Tag nobr invalid
Wed Jun 29 09:09:38 2011: file:///C:/foo/bar/doc.html:60: error: Tag nobr invalid
Wed Jun 29 09:09:38 2011: file:///C:/foo/bar/doc.html:63: error: Unexpected end tag : br
Wed Jun 29 09:09:38 2011: file:///C:/foo/bar/doc.html:85: error: Unexpected end tag : br
Wed Jun 29 09:09:39 2011: Converting document...
Wed Jun 29 09:09:39 2011: finished: success
Wed Jun 29 09:09:39 2011: ---- end
Wed Jun 29 09:09:39 2011: ---- begin
Wed Jun 29 09:09:39 2011: Loading document...
Wed Jun 29 09:09:39 2011: -:1: error: Space required after the Public Identifier
Wed Jun 29 09:09:39 2011: -:1: error: SystemLiteral " or ' expected
Wed Jun 29 09:09:39 2011: -:1: error: SYSTEM or PUBLIC, the URI is missing
Wed Jun 29 09:09:39 2011: -:11: error: Opening and ending tag mismatch: td line 9 and br
Wed Jun 29 09:09:39 2011: -:13: error: Opening and ending tag mismatch: br line 12 and td
Wed Jun 29 09:09:39 2011: -:41: error: Opening and ending tag mismatch: br line 39 and td
Wed Jun 29 09:09:39 2011: -:42: error: Opening and ending tag mismatch: td line 37 and tr
Wed Jun 29 09:09:39 2011: -:52: error: Opening and ending tag mismatch: tr line 36 and table
Wed Jun 29 09:09:39 2011: -:60: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:60: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:60: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:60: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:63: error: Opening and ending tag mismatch: p line 62 and br
Wed Jun 29 09:09:39 2011: -:65: error: Opening and ending tag mismatch: table line 35 and p
Wed Jun 29 09:09:39 2011: -:80: error: Opening and ending tag mismatch: br line 77 and font
Wed Jun 29 09:09:39 2011: -:81: error: Opening and ending tag mismatch: br line 76 and p
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:83: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:84: error: Opening and ending tag mismatch: br line 75 and font
Wed Jun 29 09:09:39 2011: -:92: error: Opening and ending tag mismatch: br line 91 and font
Wed Jun 29 09:09:39 2011: -:96: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:96: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:99: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:99: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:99: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:99: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:101: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:101: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:102: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:102: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:104: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:106: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:106: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:108: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:108: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:112: error: Entity 'ntilde' not defined
Wed Jun 29 09:09:39 2011: -:112: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:112: error: Entity 'nbsp' not defined
Wed Jun 29 09:09:39 2011: -:114: error: Opening and ending tag mismatch: br line 91 and p
Wed Jun 29 09:09:39 2011: -:115: error: Opening and ending tag mismatch: font line 90 and body
Wed Jun 29 09:09:39 2011: -:115: error: Opening and ending tag mismatch: p line 89 and html
Wed Jun 29 09:09:39 2011: -:115: error: Premature end of data in tag font line 73
Wed Jun 29 09:09:39 2011: -:115: error: Premature end of data in tag p line 72
Wed Jun 29 09:09:39 2011: -:115: error: Premature end of data in tag font line 71
Wed Jun 29 09:09:39 2011: -:115: error: Premature end of data in tag body line 5
Wed Jun 29 09:09:39 2011: -:115: error: Premature end of data in tag html line 1
Wed Jun 29 09:09:39 2011: -: error: could not load input file
Wed Jun 29 09:09:39 2011: error: no input documents to process
Wed Jun 29 09:09:39 2011: finished: failure
Wed Jun 29 09:09:39 2011: ---- end
Wed Jun 29 09:09:39 2011: ---- begin
Wed Jun 29 09:09:39 2011: Loading document...
Wed Jun 29 09:09:39 2011: Converting document...
Wed Jun 29 09:09:39 2011: finished: success
Wed Jun 29 09:09:39 2011: ---- end


Above is the log file created. The html in the first two converts are duplicates, meaning exactly the same markup just with different file names so that I would rule out any xfactor of html or css not allowing princexml to process properly. I would expect to see the exact same warnings for second convert as in first convert.
mikeday
Oh right, specify setHTML(true) for the second convert(), to ensure that it is not being parsed as XML. For the first document Prince will check the file extension, but when presented as a stream there is no file name or extension to check.