allcolor / YaHP-Converter

YaHP is a Java library that allows you to convert an HTML document into a PDF document.
GNU Lesser General Public License v2.1
56 stars 23 forks source link

Bug on parsing links in CHtmlToPdfFlyingSaucerTransformer.java 1.3 #1

Closed sankarom closed 10 years ago

sankarom commented 12 years ago

Hi, There is a bug in the "for" instruction code line 701: some links are forgotten by the algorithm. I replace the code and add a "print" media filter : final NodeList linknl = mydoc.getElementsByTagName("link"); for (int zi=0;zi<linknl.getLength();zi++) { try { int previousLength = linknl.getLength(); Element e = (Element)linknl.item(zi);

                    if ("stylesheet".equals(e.getAttribute("rel")) && "print".equals(e.getAttribute("media")) ) {
                        String url =  normalizeLink(urlForBase, e.getAttribute("href"));
                        InputStream iin = null;
                        try {
                            iin = new URL(url).openStream();
                            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
                            int iNbByteRead = -1;
                            byte bbuffer[] = new byte[16384];

                            while ((iNbByteRead = iin.read(bbuffer)) != -1) {
                                bOut.write(bbuffer, 0, iNbByteRead);
                            } // end while
                            String document = new String(bOut.toByteArray(), "utf-8");
                            CSSStyleSheet sheet = CCSSParser.parse(document, null,
                                    null, url);
                            if (sheet != null) {
                                Element style = mydoc.createElement("style");
                                style.setAttribute("type", "text/css");
                                style.setTextContent(sheet.toString());
                                e.getParentNode().insertBefore(style, e);
                                e.getParentNode().removeChild(e);
                            }
                        } // end try
                        catch (final Throwable ignore) {
                        } // end catch
                        finally {
                            try{iin.close();}catch(Throwable ignore){}
                        }

                        if( linknl.getLength() < previousLength ){
                            zi = zi - (previousLength - linknl.getLength()) ;
                        }
                    }
                }
                catch (Throwable ignore){}
            }