2014-02-20 68 views
0

如何創建pdf到gcs中,並插入一些數據?如何將內容寫入pdf? Google雲端存儲服務

這是我的代碼:

GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder() 
      .initialRetryDelayMillis(10) 
      .retryMaxAttempts(10) 
      .totalRetryPeriodMillis(15000) 
      .build()); 

      GcsFilename filename = new GcsFilename("areteaics", "test.pdf"); 
      GcsFileOptions options = new GcsFileOptions.Builder().mimeType("application/pdf").build(); 
      GcsOutputChannel writeChannel = gcsService.createOrReplace(filename,options);   
      PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8")); 
      //out.println("The woods are lovely dark and deep."); 
      //out.println("But I have promises to keep."); 
      //out.flush(); 
      writeChannel.waitForOutstandingWrites(); 
      //writeChannel.write(ByteBuffer.wrap("And miles to go before I sleep.".getBytes())); 
      writeChannel.close(); 
+0

問題是,當我嘗試打開PDF文件 - 在雲存儲的控制檯中 - 表示該文件已損壞。 – user3009757

回答

1

就像你正在創建一個PDF格式的文件(你要做的僅僅是擴展名爲PDF文件)不看。您需要使用像itext這樣的庫來創建pdf文件,然後將其寫入到cloudstorage。

e.g利用iText寫的OutputStream src

Document document = new Document(); 
    PdfWriter.getInstance(document, response.getOutputStream()); 
    document.open(); 
    document.add(new Paragraph("This is a paragraph")); 
    document.close(); 

你需要修改上面的代碼寫入到GCS(而不是響應的OutputStream)。

相關問題