2012-10-11 90 views
8

是否可以使用Google Drive API將HTML文件上傳並轉換爲PDF,而不使用user interaction使用Google Drive API的HTML2PDF

+1

那你試試?你有沒有搜索?您是否閱讀過API文檔? – Jocelyn

+0

@Jocelyn Proppy在谷歌工作,所以我會大膽猜測,並說他可能在發佈之前嘗試了明顯的東西。 –

+0

@NickJohnson嗯,我們不是巫師,我們該如何猜測?每個在Stackoverflow上發佈問題的人都會展示他嘗試的內容,他已經在搜索的內容。 – Jocelyn

回答

2

工作對我來說(驅動器的文檔只...)

ByteArrayContent mediaContent = new ByteArrayContent("text/html", "HTML PAGE HERE".getBytes()); 

File body = new File(); 
body.setTitle("test.html"); 
body.setMimeType("text/html"); 

Insert request = null; 
try 
{ 
    request = service.files().insert(body, mediaContent); 
    request.setConvert(true); 
    File file = request.execute(); 

    HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getExportLinks().get("application/pdf"))).execute(); 

    OutputStream out = new FileOutputStream(getExternalFilesDir(null).getAbsolutePath() + "/test.pdf"); 
    byte[] buf = new byte[1024]; 
    int len; 
    while ((len = resp.getContent().read(buf)) > 0) 
    { 
     out.write(buf, 0, len); 
    } 
    out.close(); 

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