Frage

I am trying to access image from DB and render it in PDF. Below is my servlet to get Image from DB.

public class ImageDownloadServlet extends HttpServlet 
{
private  Connection connection = null;
private Statement stmt = null;
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
 try {
Integer imageId=Integer.parseInt(req.getParameter("imageId").toString());


            connection = //creating connection  
                stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT IMG_BLOB FROM ImageTable WHERE img_id = "+imageId);
        rs.next();
        Blob b=rs.getBlob(1);

        resp.setContentType("image/jpeg");
        resp.setContentLength((int) b.length());
        InputStream is = b.getBinaryStream();
        OutputStream os = resp.getOutputStream();
        byte buf[] = new byte[(int) b.length()];
        is.read(buf);
        os.write(buf);
        os.close();

        stmt.close();
        connection.close();


    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }


}
}

This code is working fine and i am getting image in CKEditor. But while trying to create PDF this servlet is not being called. My web.xml is <servlet> <description></description> <display-name>ImageDownloadServlet</display-name> <servlet-name>ImageDownloadServlet</servlet-name> <servlet-class>test.ImageDownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageDownloadServlet</servlet-name> <url-pattern>/tesing/Image</url-pattern> </servlet-mapping>

Amd my code to create PDF is

File file = new File(D:/satz/sat.pdf);
    FileOutputStream fos=new FileOutputStream(file);
    Document doc=new Document(PageSize.A4, 50, 50, 70, 130);
    PdfWriter pdfWriter=PdfWriter.getInstance(doc, fos);
    String pdfString="<html><body>hello<img src='/tesing/Image?imageId=1' alt='' /></body></html>";
    doc.open();
    XMLWorkerHelper worker=XMLWorkerHelper.getInstance();
    ByteArrayInputStream is = new ByteArrayInputStream(pdfString.getBytes());
    worker.parseXHtml(pdfWriter, doc, is);
    doc.close();

My problem is while creating the PDF its not calling this servlet. I am using itextpdf-5.4.4.jar and xmlworker-5.4.1.jar and spring MVC.

UPDATED If I use RAD and Tomcat 7.0 server the full path is calling my servlet and generating PDF with image. But when i am using RAD and Websphere 8.5 it's not at all invoking my servlet.

War es hilfreich?

Lösung

You try to parse this HTML

String pdfString="<html><body>hello<img src='/tesing/Image?imageId=1' alt='' /></body></html>";

Your image source URL does neither include protocol nor host. How should iText know how (which protocol) and where (which host) to request your partial URL.

Thus, use the complete URL instead.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top