2017-02-06 70 views
0

我正在谷歌應用引擎上運行一個應用。如何從應用引擎上的GCS獲取pdf文件 - java

試圖從谷歌雲存儲的PDF文件中獲取TXT文件。

當我在本地運行我的代碼,它成功,但AppEngine上運行時,它失敗org.pdfbox.exceptions.WrappedIOException

這裏是我的代碼:

import com.google.cloud.storage.*; 
import org.pdfbox.pdfparser.PDFParser; 
import org.pdfbox.pdmodel.PDDocument; 
import org.pdfbox.util.PDFTextStripper; 


public class Download { 

    public static String perform(String bucket, String file) throws IOException { 
     byte[] fileByte = download(bucket, file); 
     String pdfFileTxt = pdf2txt(fileByte); 
     return pdfFileTxt; 

    } 

    public static byte[] download(String bucketName, String fileId) throws IOException { 
     Storage storage = StorageOptions.getDefaultInstance().getService(); 
     BlobId blobId = BlobId.of(bucketName, fileId); 
     Blob blob = storage.get(blobId); 
     return blob.getContent(); 
    } 

    public static String pdf2txt(byte[] byteArr) throws IOException { 
     InputStream stream = new ByteArrayInputStream(byteArr); 
     PDFParser parser = new PDFParser(stream); 
     parser.parse(); 
     PDDocument pdDoc = new PDDocument(parser.getDocument()); 
     return new PDFTextStripper().getText(pdDoc); 
    } 
} 

代碼上parser.parse();org.pdfbox.exceptions.WrappedIOException失敗 - 沒有其他消息添加:(

從存儲器下載 - 實際上成功。如果我登錄數據,我得到像這樣的東西:

%PDF-1.3 
%���� 
7 0 obj 
<</Linearized 1/L 7945/O 9/E 3524/N 1/T 7656/H [ 451 137]>> 
endobj 

13 0 obj 
<</DecodeParms<</Columns 4/Predictor 12>>/Filter/FlateDecode/ID[<4DC91A1875A6D707AEC203BB021C93A0><F6C92B368A8A13408457A1D395A37EB9>]/Index[7 21]/Info 6 0 R/Length 52/Prev 7657/Root 8 0 R/Size 28/Type/XRef/W[1 2 1]>>stream 
h�bbd``b`� ��H0� 6G ��#�4�,#��Ɲ_ L�� 
endstream 
endobj 
startxref 
0 
%%EOF 
... more ... 

現在有無論如何克服這一點?也許使用不同的庫?由於代碼在appengine上運行,因此跟蹤這些錯誤非常困難。

+0

的可能的複製[下載PDF文件,並得到有關谷歌的AppEngine雲存儲成txt(http://stackoverflow.com/questions/42071045/download- pdf文件,和得到最TXT從 - 谷歌 - 雲存儲 - 的AppEngine) – Aaron

回答

0

PdfBox does not run on GAE。它使用不允許的Java類。

作爲一項工作,你可以下載一個修改後的pdfbx jar。 感謝icyerasor的answer和使用these說明。

這裏有完整的說明:

下載此folder 現在cd到你的項目dirctory並運行以下命令:

mkdir local-maven-repo 

mvn deploy:deploy-file -DgroupId=org.apache.pdfbox -DartifactId=pdfbox -Dversion=1.8.0-SNAPSHOT -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=/your/path/to/download/directory/pdfbox-GAE/pdfbox-1.8.0-SNAPSHOT.jar 

mvn deploy:deploy-file -DgroupId=org.apache.pdfbox -DartifactId=fontbox -Dversion=1.8.0-SNAPSHOT -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=/your/path/to/download/directory/pdfbox-GAE/dependencies/fontbox-1.8.0-SNAPSHOT.jar 
下項目POM

補充:

<repositories> 
    <repository> 
     <id>local-maven-repo</id> 
     <url>file:///${project.basedir}/local-maven-repo</url> 
    </repository> 
</repositories> 

現在在POM編輯你的依賴:

 <dependency> 
     <groupId>commons-logging</groupId> 
     <artifactId>commons-logging</artifactId> 
     <version>1.1.1</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging-api --> 
    <dependency> 
     <groupId>commons-logging</groupId> 
     <artifactId>commons-logging-api</artifactId> 
     <version>1.1</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging-adapters --> 
    <dependency> 
     <groupId>commons-logging</groupId> 
     <artifactId>commons-logging-adapters</artifactId> 
     <version>1.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.pdfbox</groupId> 
     <artifactId>fontbox</artifactId> 
     <version>1.8.0-SNAPSHOT</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.pdfbox</groupId> 
     <artifactId>pdfbox</artifactId> 
     <version>1.8.0-SNAPSHOT</version> 
    </dependency> 

最後的工作代碼:

import com.google.cloud.storage.*; 
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.util.PDFTextStripper; 

public class Download { 

    public static String perform(String bucket, String file) throws Exception { 
     byte[] fileByte = download(bucket, file); 
     String pdfFileTxt = pdf2txt2(fileByte); 
     return pdfFileTxt; 
    } 

    public static byte[] download(String bucketName, String fileId) throws IOException { 
     Storage storage = StorageOptions.getDefaultInstance().getService(); 
     BlobId blobId = BlobId.of(bucketName, fileId); 
     Blob blob = storage.get(blobId); 
     return blob.getContent(); 
    } 


    public static String pdf2txt2(byte[] byteArr) throws IOException { 
     InputStream myInputStream = new ByteArrayInputStream(byteArr); 
     PDDocument pddDoc = PDDocument.load(myInputStream); 
     PDFTextStripper reader = new PDFTextStripper(); 
     String pageText = reader.getText(pddDoc); 
     pddDoc.close(); 
     return pageText; 
    } 
} 
相關問題