关于转化API的Google App Engine文档不完整 [英] Incomplete Google App Engine Documentation on Conversion API

查看:75
本文介绍了关于转化API的Google App Engine文档不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将HTML字符串转换为.PNG文件。感谢说资产的名称应该匹配img标签的src属性。



这是我在交互式控制台中尝试的一段python代码,并且它实际上从google.appengine.api生成了两张图片

 从google.appengine.api导入urlfetch 
导入转换

url =http://a0.twimg.com/profile_images/77186109/favicon_reasonably_small.png
asset = conver sion.Asset(text / html,'< b>一些数据< / b>< br />< img src =a.png/>< img src =%s/ >< br /> Foo'%url,test.html)
conversion_obj = conversion.Conversion(资产,image / png)

img_response = urlfetch.fetch (url)
image1 = conversion.Asset('image / png',img_response.content,a.png)
image2 = conversion.Asset('image / png',img_response.content,url )
conversion_obj.add_asset(image1)
conversion_obj.add_asset(image2)

结果= conversion.convert(conversion_obj)
打印result.error_text

print repr(result.assets [0] .data)


I am trying to convert a HTML string into a .PNG file. Thanks to Nick Johnson who pointed me to GAE Conversion API see previous post Here.

I use the GAE Conversion API which works but my images in my HTML string doesn't display.

In the Documentation on GAE Conversion API there is a reference to Adding Assets and a mention of static/icon.gif which I think may address my problem but that documentation is incomplete.

Can anyone please assist?

My HTML conversion to .PNG works but all images e.g

<img src="http://mydomain.com/image.gif> doesn't print yet http://mydomain.com/image.gif is available.

Thanks

PB

Update 1: According to the documentation, I should create seperate asset for each image and name must match the src attr.

 List<Asset>assets = new ArrayList<Asset>();    
        try {
            Asset asset = new Asset("text/html", notification.getMessage().getBytes("UTF-8"), "testfile.html");
            assets.add(asset);
        } catch (UnsupportedEncodingException e) {      
            e.printStackTrace();
        }

winningNumbers is just a number, expecting 5 0r 6 numbers

    for (String n : winningNumbers) {
                String url = "http://www.someURL.com/img/balls/" + n +".gif";
                Asset e = new Asset("image/gif", loadGIFByte(url), url);            
                assets.add(e); 

}




 Document document = new Document(assets);  
//Here I want to convert everything, the images and html to a .PNG MIMETYPE.    
            Conversion conversion = new Conversion(document, "image/png");
            ConversionService service =          ConversionServiceFactory.getConversionService();
            ConversionResult conversionResult = service.convert(conversion);


private byte[] loadGIFByte(String urlGIF) {
    System.out.println("Loading GIF for "+ urlGIF);
    OutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1536];
    int count = 0;
    try {
        URL u = new URL(urlGIF);
        InputStream reader = u.openStream();
        while ((count = reader.read(buf)) >= 0) {
            out.write(buf, 0, count);           
            }
    } catch (MalformedURLException e) {

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

        e.printStackTrace();
    }   

return buf;
}
    }   

Update 2:
A typical notification.getMessage() should return;

<html> 
<head> 
<style type="text/css"> 
/** Add css rules here for your application. *//** Example rules used by the template application (remove for your app) */h1 {  font-size: 2em;  font-weight: bold;  color: #777777;  margin: 40px 0px 70px;  text-align: center;}.sendButton {  display: block;  font-size: 16pt;}/** Most GWT widgets already have a style name defined */.gwt-DialogBox {  width: 400px;}.dialogVPanel {  margin: 5px;}.serverResponseLabelError {  color: red;}/** Set ids using widget.getElement().setId("idOfElement") */#closeButton {  margin: 15px 6px 6px;}.usefulLinksUL{list-style-type:none;margin:0;padding:0;display:block;font-weight:bold;#background-color:#FFD700;width:180px;}.usefulLinksAnchor{a:link,a:visited;text-decoration:none;a:hover,a:active;}.note {font-size: 80%;background-color: #FEF49E;/**background-color: #d8da3d;*/border: 1px solid #D0C98D;-webkit-box-shadow: -8px 5px 5px rgba(0, 0, 0, 0.3);}.note-title {margin: 5px 5px 0 5px;padding: 4px;-moz-border-radius: 5px;-webkit-border-radius: 5px;background-color: #D0C98D;color: white;font-weight: bolder;}.note-content {margin: 5px;background: transparent;border: none;overflow: hidden;}.container { width:50px; overflow:hidden }.cube { width:150px; height:150px; float:left;}#rounded-corner{  font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;    font-size: 12px;    margin: 5px;    /*width: 180px;*/   text-align: left;   border-collapse: collapse;}#rounded-corner thead th.rounded-company{    background: #b9c9fe url('img/latestResult/left.png') left -1px no-repeat;}#rounded-corner thead th.rounded-q4{  background: #b9c9fe url('img/latestResult/right.png') right -1px no-repeat;}#rounded-corner th{ padding: 8px;   font-weight: normal;    font-size: 13px;    color: #039;    background: #b9c9fe;}#rounded-corner td{    padding: 8px;   background: #e8edff;    border-top: 1px solid #fff; color: #669;}#rounded-corner tfoot td.rounded-foot-left{    background: #e8edff url('img/latestResult/botleft.png') left bottom no-repeat;}#rounded-corner tfoot td.rounded-foot-right{ background: #e8edff url('img/latestResult/botright.png') right bottom no-repeat;}#rounded-corner tbody tr:hover td{ background: #d0dafd;}.latestLotto{  background-color: #C3D9FF;  border: 1px solid #87B3FF;  cursor: default; }.bbottomyello {border-bottom: 1px solid #CFA55B;}.onGreenBackground {font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: white;font-style: normal;}.btopbottomYellow {border-top: 1px solid #FFCB05;border-bottom: 1px solid #FFCB05;}.bbottomYellow {border-bottom: 1px solid #FFCB05;}h3 {font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: #EB0900;} 
</style> 
</head> 
<body> 

<table width="100%" height="123" border="0" cellpadding="0" cellspacing="0" bgcolor = "#FFFFFF"> 
            <tbody><tr> 
                <td width="47%" class="bbottomyello"><img src="http://mylottoshop.appspot.com//img/lotto_top_left_home.gif" alt=""></td> 
                <td width="16%" class="bbottomyello">&nbsp;</td> 
                <td colspan="3" valign="top" class="bbottomYellow">&nbsp;</td> 
            </tr> 
            <tr> 
                <td valign="top" bgcolor="#00A452">&nbsp;&nbsp;<span class="onGreenBackground"><span class="onGreenBackground">Wednesday, June 13 2012</span></span></td> 
                <td colspan="4" bgcolor="#00A452" class="onGreenBackground">Latest Results: Confirmed</td> 
            </tr> 
            <tr> 
                <td height="23" colspan="5" class="btopbottomYellow">&nbsp;<strong>&nbsp;Winning Numbers</strong></td> 
            </tr> 
            <tr> 
                <td height="31" colspan="2" class="bbottomYellow"> 
                <div align="center"> 
                    <img src="http://mylottoshop.appspot.com//img/balls/9.gif" width="30" height="40">  
                    <img src="http://mylottoshop.appspot.com//img/balls/20.gif" width="30" height="40">  
                    <img src="http://mylottoshop.appspot.com//img/balls/14.gif" width="30" height="40">  
                    <img src="http://mylottoshop.appspot.com//img/balls/16.gif" width="30" height="40">  
                    <img src="http://mylottoshop.appspot.com//img/balls/43.gif" width="30" height="40">  
                    <img src="http://mylottoshop.appspot.com//img/balls/41.gif" width="30" height="40"> 
                    </div> 
                    </td> 
                <td width="12%" height="31" valign="middle" class="bbottomYellow"><h3>Bonus<br> 
                    Ball:</h3></td> 
                <td width="14%" class="bbottomYellow"> <div align="center"><img src="http://mylottoshop.appspot.com//img/balls/13.gif" width="34" height="40"></div></td>                 
            </tr> 
            </tbody></table></body> 

</html>

Notification class signature is:

public class Notification implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -8464293752450599178L;
    private Result result;
    private NotificationClientType client;
    private String message;
    private String api_key;
    private String secret_key;
    private String deliveryAddress; //e.g email, fax etc
    public NotificationClientType getClient() {
        return client;
    }
    public void setClient(NotificationClientType client) {
        this.client = client;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getApi_key() {
        return api_key;
    }
    public void setApi_key(String api_key) {
        this.api_key = api_key;
    }
    public String getSecret_key() {
        return secret_key;
    }
    public void setSecret_key(String secret_key) {
        this.secret_key = secret_key;
    }
    public String getDeliveryAddress() {
        return deliveryAddress;
    }
    public void setDeliveryAddress(String deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
    }
    public Result getResult() {
        return result;
    }
    public void setResult(Result result) {
        this.result = result;
    }

Some Feedback. No difference in result. I guess return out.toByteArray() solved NPE which I accidentally caused.

The expected result is below: The same HTML string sent as email yields:

GAE conversion result is:

解决方案

The documentation says that the name of the asset should match the src attribute of the img tag.

Here is a python code I tried in the interactive console, and it actually produced the two images

from google.appengine.api import urlfetch
from google.appengine.api import conversion

url = "http://a0.twimg.com/profile_images/77186109/favicon_reasonably_small.png"
asset = conversion.Asset("text/html", '<b>some data</b><br/><img src="a.png"/><img src="%s"/><br/>Foo' % url, "test.html")
conversion_obj = conversion.Conversion(asset, "image/png")

img_response = urlfetch.fetch(url)
image1 = conversion.Asset('image/png', img_response.content, "a.png")
image2 = conversion.Asset('image/png', img_response.content, url)
conversion_obj.add_asset(image1)
conversion_obj.add_asset(image2)

result = conversion.convert(conversion_obj)
print result.error_text

print repr(result.assets[0].data)

这篇关于关于转化API的Google App Engine文档不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆