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

exception while running YaHP-sample #4

Closed pudiganapathi closed 12 years ago

pudiganapathi commented 12 years ago

i directly give only url="http:/www.google.com" and outfile="test.pdf" run then get this error Initializing... Installing new URLStreamHandlerFactory... Installing new URLStreamHandlerFactory DONE. init time: 113 before conversion after conversion Destroying YAHP ClassLoader Tree Exception in thread "AWT-Windows" java.lang.IllegalStateException: Shutdown in progress at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:39) at java.lang.Runtime.addShutdownHook(Runtime.java:192) at sun.awt.windows.WToolkit$2.run(WToolkit.java:282) at java.security.AccessController.doPrivileged(Native Method) at sun.awt.windows.WToolkit.run(WToolkit.java:267) at java.lang.Thread.run(Thread.java:662)

allcolor commented 12 years ago

Add a thread sleep afer calling, ut's because the conversion is going quicker than swing thread and you run it in the main thread.

Swing register a shutdown hook but when it does it, your main thread is already finished.

syarabolu commented 11 years ago

I have the same issue.Where Do I need to exactly add the thread.sleep() and how many millis I need to I it's a big Document to convert?

I have the following Code

public class PdfConversion {

@SuppressWarnings({ "unchecked", "rawtypes", "static-access" })
public static void main(String[] args) {

try{    
        CYaHPConverter converter = new CYaHPConverter();
        URL url=new URL("C:\\Users\\Desktop\\Example.html");
        File fout = new File("C:\\applicaitonId\\red123.pdf");
        FileOutputStream out = new FileOutputStream(fout);
        Map properties = new HashMap();
        List headerFooterList = new ArrayList();

        String str = "<HTML><HEAD></HEAD><BODY><H1>Testing</H1><FORM>" +
                     "check : <INPUT TYPE='checkbox' checked=checked/><br/>"   +
                     "</FORM></BODY></HTML>";

        properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
                       IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
        Thread th=new Thread();
        th.sleep(100000);
        converter.convertToPdf(url,IHtmlToPdfTransformer.A4P,headerFooterList,out,properties);
        out.flush();
        out.close();
}catch(Exception e){
    e.printStackTrace();
}
}

}

allcolor commented 11 years ago

Well in fact, on destroy, there is a bug in the AWT/Swing implementation, and AWT try to get a Graphics2D impl and stupidly want to add a shutdown hook which at the time is too late (JVM is shutting down). You can safely ignore that exception (the pdf is generated). It's not a problem, I'm not working actively on this project anymore, but if it is really a problem for you, I could try to correct it, contact me for that. Regards, Quentin

syarabolu commented 11 years ago

Hi, I am not able to get the radio buttons in my PDF document.How can I make it work?

allcolor commented 11 years ago

Could you send me a sample document which does not work for me to test ?

Could you send it to "quentin.anciaux@allcolor.org" ?

Regards, Quentin

2012/11/9 freakyprogrammer notifications@github.com

Hi, I am not able to get the radio buttons in my PDF document.How can I make it work?

— Reply to this email directly or view it on GitHubhttps://github.com/allcolor/YaHP-Converter/issues/4#issuecomment-10216876.

All those moments will be lost in time, like tears in rain.

puntri commented 11 years ago

Hi

Please help me , the code hand after exception

/*

import java.io.File; import java.io.FileOutputStream; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.allcolor.yahp.converter.CClassLoader; import org.allcolor.yahp.converter.CYaHPConverter; import org.allcolor.yahp.converter.IHtmlToPdfTransformer;

public class tttt { public static void main(String ... args ) throws Exception { htmlToPdfFile(); }

public static void htmlToPdfFile() throws Exception { CYaHPConverter converter = new CYaHPConverter(); File fout = new File("d:/x.pdf"); FileOutputStream out = new FileOutputStream(fout); Map properties = new HashMap(); List headerFooterList = new ArrayList();

String str = "<HTML><HEAD></HEAD><BODY><H1>Testing</H1><FORM>" +
             "check : <INPUT TYPE='checkbox' checked=checked/><br/>"   +
             "</FORM></BODY></HTML>";

properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
               IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
//properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, fontPath);
converter.convertToPdf(str,
      IHtmlToPdfTransformer.A4P,
      headerFooterList,
      "file:///temp/", // root for relative external CSS and IMAGE
      out,
      properties);

    try {

Field f = Class.forName("java.lang.ApplicationShutdownHooks") .getDeclaredField("hooks"); f.setAccessible(true); IdentityHashMap l = (IdentityHashMap) f.get(null); for (Iterator it = l.entrySet().iterator(); it.hasNext();) { Entry e = (Entry) it.next(); Thread o = (Thread) e.getKey(); if ((o != null) && (o.getClass().getClassLoader() != null) && (o.getClass().getClassLoader().getClass() == CClassLoader.class)) { it.remove(); continue; } o = (Thread) e.getValue(); if ((o != null) && (o.getClass().getClassLoader() != null) && (o.getClass().getClassLoader().getClass() == CClassLoader.class)) { it.remove(); } } } catch (Throwable t) { System.err.println("An error occurs while converting '" + "'. Cause : " + t.getMessage());

}

Thread.currentThread().interrupt();
out.flush();
out.close();

} }

Initializing... Installing new URLStreamHandlerFactory... Installing new URLStreamHandlerFactory DONE. init time: 31 Destroying YAHP ClassLoader Tree Exception in thread "AWT-Windows" java.lang.IllegalStateException: Shutdown in progress at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66) at java.lang.Runtime.addShutdownHook(Runtime.java:209) at sun.awt.windows.WToolkit$2.run(WToolkit.java:279) at java.security.AccessController.doPrivileged(Native Method) at sun.awt.windows.WToolkit.registerShutdownHook(WToolkit.java:264) at sun.awt.windows.WToolkit.run(WToolkit.java:290) at java.lang.Thread.run(Thread.java:722)

allcolor commented 11 years ago

This bug is specific on windows.

So to correct it, try to do the conversion in another thread, and in the main, join on that thread (thread.join()).

I don't use windows at all, so I can't replicate.

Regards, Quentin

2013/1/29 puntri notifications@github.com

Hi

Please help me , the code hand after exception

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor. */

import java.io.File; import java.io.FileOutputStream; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.allcolor.yahp.converter.CClassLoader; import org.allcolor.yahp.converter.CYaHPConverter; import org.allcolor.yahp.converter.IHtmlToPdfTransformer;

public class tttt { public static void main(String ... args ) throws Exception { htmlToPdfFile(); }

public static void htmlToPdfFile() throws Exception { CYaHPConverter converter = new CYaHPConverter(); File fout = new File("d:/x.pdf"); FileOutputStream out = new FileOutputStream(fout); Map properties = new HashMap(); List headerFooterList = new ArrayList();

String str = "

Testing

" + "check :
" + "
";

properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER); //properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, fontPath); converter.convertToPdf(str, IHtmlToPdfTransformer.A4P, headerFooterList, "file:///temp/", // root for relative external CSS and IMAGE out, properties);

try {

Field f = Class.forName("java.lang.ApplicationShutdownHooks") .getDeclaredField("hooks"); f.setAccessible(true); IdentityHashMap l = (IdentityHashMap) f.get(null); for (Iterator it = l.entrySet().iterator(); it.hasNext();) { Entry e = (Entry) it.next(); Thread o = (Thread) e.getKey(); if ((o != null) && (o.getClass().getClassLoader() != null) && (o.getClass().getClassLoader().getClass() == CClassLoader.class)) { it.remove(); continue; } o = (Thread) e.getValue(); if ((o != null) && (o.getClass().getClassLoader() != null) && (o.getClass().getClassLoader().getClass() == CClassLoader.class)) { it.remove(); } } } catch (Throwable t) { System.err.println("An error occurs while converting '" + "'. Cause : " + t.getMessage());

}

Thread.currentThread().interrupt(); out.flush(); out.close();

} }

Initializing... Installing new URLStreamHandlerFactory... Installing new URLStreamHandlerFactory DONE. init time: 31 Destroying YAHP ClassLoader Tree

Exception in thread "AWT-Windows" java.lang.IllegalStateException: Shutdown in progress at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66) at java.lang.Runtime.addShutdownHook(Runtime.java:209) at sun.awt.windows.WToolkit$2.run(WToolkit.java:279) at java.security.AccessController.doPrivileged(Native Method) at sun.awt.windows.WToolkit.registerShutdownHook(WToolkit.java:264) at sun.awt.windows.WToolkit.run(WToolkit.java:290) at java.lang.Thread.run(Thread.java:722)

— Reply to this email directly or view it on GitHubhttps://github.com/allcolor/YaHP-Converter/issues/4#issuecomment-12847028.

All those moments will be lost in time, like tears in rain.