Hi all,
I'm having trying to use iText (v5.2) & the XMLWorker (v1.1.2) tool to
generate a PDF. Trouble is that the resulting PDF doesn't appear to be
using any of the CSS styles defined on the page. Images are there but
no CSS. I've followed the XMLWorker documentation from
http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html but it's
looking out of date with the current version. The main problem at the
moment at the moment is this line in the documentation:
HtmlPipelineContext htmlContext = new HtmlPipelineContext();
With the versions I am using, the constructor of HtmlPipelineContext
requires a CssAppliers class. I'm supplying a null object for now as I
have no idea how to get my hands on a CssAppliers class but could this
be what is causing my PDF to come out with no CSS styles applied? Does
anyone have any idea how I get my hands on a CssAppliers instance? The
API docs didn't shed much light on the situation.
Can anyone help me get past this step? I've attempted to attach my test
class so you can see what I'm trying to do. No idea how attachments
work with this mailing list though...
Regards,
Lee
package com.oobjects.kbroker.pdf;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
import com.itextpdf.tool.xml.pipeline.html.LinkProvider;
public class XMLWorkerTest {
private static final Log LOG = LogFactory.getLog(XMLWorkerTest.class);
private static final HttpClient httpClient = new HttpClient();
private static final String URL =
"http://socialcareconnect.barnet.gov.uk/kb5/barnet/home.page";
private static final String URL_BASE =
"http://socialcareconnect.barnet.gov.uk/kb5/barnet/";
private static String htmlSource = null;
@BeforeClass
public static void setUp() throws HttpException, IOException {
GetMethod getMethod = new GetMethod(URL);
httpClient.executeMethod(getMethod);
htmlSource = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
}
@Test
public void advancedConvertURLToPDF() {
OutputStream fos = null;
InputStream bis = null;
try {
File tempPDF = File.createTempFile("adv-", ".pdf");
fos = new FileOutputStream(tempPDF);
LOG.info("Creating PDF at " + tempPDF.getAbsolutePath());
bis = new ByteArrayInputStream(htmlSource.getBytes("UTF-8"));
FontFactory.registerDirectories();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext();
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new AbstractImageProvider() {
public String getImageRootPath() {
return URL_BASE;
}
});
htmlContext.setLinkProvider(new LinkProvider() {
public String getLinkRoot() {
return URL_BASE;
}
});
CSSResolver cssResolver =
XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new
HtmlPipeline(htmlContext, new PdfWriterPipeline(
document, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
p.parse(bis);
document.close();
} catch (IOException e) {
LOG.error(e, e);
} catch (DocumentException e) {
LOG.error(e, e);
} finally {
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(bis);
}
}
}------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php