转化API上不完整的Google App Engine文档

时间:2012-05-25 18:05:22

标签: google-app-engine

我正在尝试将HTML字符串转换为.PNG文件。感谢Nick Johnson指出我GAE Conversion API,请参阅上一篇文章Here

我使用GAE Conversion API 哪个有效,但我的HTML字符串中的图像无法显示。

GAE Conversion API上的文档中,提及Adding Assets并提及static/icon.gif,我认为这可能会解决我的问题,但文档不完整。

有人可以帮忙吗?

我的HTML转换为.PNG,但所有图片都可用,例如

<img src="http://mydomain.com/image.gif>尚未打印http://mydomain.com/image.gif可用。

由于

PB

更新1: 根据文档,我应该为每个图像创建单独的资产,名称必须与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();
        }

winnerNumbers只是一个数字,期待5 0r 6个数字

    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;
}
    }   

更新2:
典型的notification.getMessage()应该返回;

<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>

通知类签名是:

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;
    }

一些反馈。 结果没有区别。我想返回out.toByteArray()解决了我偶然造成的NPE。

预期结果如下:与电子邮件一起发送的相同HTML字符串产生: expected result

GAE转换结果为:

enter image description here

1 个答案:

答案 0 :(得分:1)

documentation表示资产名称应与img标记的src属性匹配。

这是我在交互式控制台中尝试的python代码,它实际上产生了两个图像

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)