Thanks for the code Raj, saved me.

If it's useful to anyone else, based on your sample I wrote a general
purpose URL->PDF function that will search the HTML for any dependent
images and insert them, only external dependency is commons.io

-Casey

            String content = IOUtils.toString(new
URL(url).openStream());
            Asset asset = new Asset("text/html", content.getBytes(),
"in.html");

            Matcher m = Pattern.compile("<img[^>]*src=\"([^\"]*)",
Pattern.CASE_INSENSITIVE).matcher(content);

            List<Asset> assetsList = new ArrayList<Asset>();
            assetsList.add(asset);

            while (m.find()) {
                String relative = m.group(1);
                URL absolute = new URL(new URL(url), relative);
                byte[] image_data =
IOUtils.toByteArray(absolute.openStream());

                String guessedContentType = null;
                if (relative.toLowerCase().endsWith(".png")) {
                    guessedContentType = "image/png";
                } else if (relative.toLowerCase().endsWith(".jpg")) {
                    guessedContentType = "image/jpeg";
                } else if (relative.toLowerCase().endsWith(".gif")) {
                    guessedContentType = "image/gif";
                }

                Asset subAsset = new Asset(guessedContentType == null
|| response.getContentType() != null ? response.getContentType() :
guessedContentType, image_data, relative);
                assetsList.add(subAsset);
            }

            Document document = new Document(assetsList);
            Conversion conversion = new Conversion(document,
"application/pdf");
            ConversionService service =
ConversionServiceFactory.getConversionService();
            ConversionResult result = service.convert(conversion);

            if (result.success()) {
 
response.getOutputStream().write(result.getOutputDoc().getAssets().get(0).getData());
            } else {
                logger().severe(result.getErrorCode().name());
 
response.getOutputStream().write(result.getErrorCode().name().getBytes());
            }

On Jan 5, 10:23 am, RAJ <er.rajes...@gmail.com> wrote:
> I have solved this issue now :) (Java Documentation must be updated
> with right example)
>
> <code>
>
> Asset asset = new Asset(
>     "text/html", "<b>some data</b><img src="static/icon.gif"/
>
> >".getBytes(), "testfile.html");
>
> byte[] image_data =  // Read yourimageinto bytes[] array;
>
> Asset subAsset = new Asset(
>     "image/gif", image_data, "static/icon.gif");
>
> List<Asset> assetsList = new ArrayList<Asset>();
> assetsList.add(asset);
> assetsList.add(subAsset);
>
> Document document = new Document(assetsList);
>
> </code>
>
> Cheers,
> Raj
>
> On Jan 4, 5:31 pm, RAJ <er.rajes...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi All,
>
> > I am trying to producepdfreports out of my html String as mentioned
> > in below tutorial:
>
> >http://code.google.com/appengine/docs/java/conversion/overview.html
>
> > <code>
>
> > // Create a conversion request from HTML to PNG.
> > Asset asset = new Asset(
> >     "text/html", "<b>some data</b>".getBytes(), "testfile.html");
> > Document document = new Document(asset);
>
> > </code>
>
> > My HTML String has few pictures to be embedded. Thus, I am trying
> > "Additional Functions (Adding Assets)" example to add images as sub
> > asset.
>
> > But, example mentioned in tutorial is wrong. I am not able to add
> >imageas sub-asset in my main html string. There is no java api
> > mentioned for the same!
>
> > Has someone done this earlier using Java?  (Example in python is
> > correctly described)
>
> > Cheers,
> > Raj

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to