I do this by rendering the HTML into a PDF, and then rasterizing the PDF to an image. I've tried using several implementations, but currently I use PD4ML for the HTML rendering, and JPedal for rasterizing the PDF.

-------------- Step 1 -----------------

        /**
         * Uses PD4ML to generate a PDF-file from HTML.
         *
         * @param htmlString The HTML to work from.
         * @param isLandscape
         * @return Rendered PDF-data.
         */
public static NSData convertHTMLToPdf( String htmlString, boolean isLandscape ) {

                java.awt.Dimension format = PD4Constants.A4;
                boolean landscapeValue = isLandscape;
                int topValue = 10;
                int leftValue = 10;
                int rightValue = 10;
                int bottomValue = 10;
                String unitsValue = "mm";
                //              boolean patchValue = true;
                boolean splitValue = true;
                String proxyHost = "";
                int proxyPort = 0;
                int userSpaceWidth = 780;

                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                try {
if( proxyHost != null && proxyHost.length() != 0 && proxyPort != 0 ) {
                                System.getProperties().setProperty( "proxySet", 
"true" );
                                System.getProperties().setProperty( 
"proxyHost", proxyHost );
                                System.getProperties().setProperty( "proxyPort", 
"" + proxyPort );
                        }

                        PD4ML pd4ml = new PD4ML();
                        if( landscapeValue ) {
                                format = pd4ml.changePageOrientation( format );
                        }

                        pd4ml.setPageSize( format );

                        if( unitsValue.equals( "mm" ) ) {
pd4ml.setPageInsetsMM( new java.awt.Insets( topValue, leftValue, bottomValue, rightValue ) );
                        }
                        else {
pd4ml.setPageInsets( new java.awt.Insets( topValue, leftValue, bottomValue, rightValue ) );
                        }

                        pd4ml.setHtmlWidth( userSpaceWidth );
                        pd4ml.enableImgSplit( splitValue );

                        pd4ml.render( new StringReader( htmlString ), bos );
                }
                catch( Exception e ) {
                        logger.error( "Could not convert HTML to PDF", e );
                }

                return new NSData( bos.toByteArray() );
        }


-------------- Step 2 -----------------

        /**
* Rasterizes PDF-data into JPEG-data, returning null if the conversion is not successful.
         */
        public static NSData convertPDFToJPEGData( NSData data ) {
                try {
                        PdfDecoder decoder = new PdfDecoder();
                        decoder.setExtractionMode( 0, 72, 2.0f );
                        decoder.openPdfArray( data.bytes() );
                        decoder.decodePage( 1 );

                        BufferedImage image = decoder.getPageAsImage( 1 );
                        return 
USImageUtilities.encodeImageToJPEGDataWithQuality( image, 1 );

                }
                catch( Exception e ) {
logger.error( "An esception occurred during rasterizing of PDF data to JPEG", e );
                        return null;
                }
        }

-------------------------------

Cheers,
- hugi



On 30.3.2009, at 15:31, Kieran Kelleher wrote:

Anyone have any suggestions for free utility to make thumbnail (or plain image that I can thumbnail) of a HTML page in java?

-Kieran
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/hugi%40karlmenn.is

This email sent to [email protected]

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to