View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.parser.pdf;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Calendar;
22  import java.util.Collections;
23  import java.util.Set;
24  
25  import org.apache.pdfbox.pdmodel.PDDocument;
26  import org.apache.pdfbox.pdmodel.PDDocumentInformation;
27  import org.apache.tika.exception.TikaException;
28  import org.apache.tika.metadata.Metadata;
29  import org.apache.tika.mime.MediaType;
30  import org.apache.tika.parser.ParseContext;
31  import org.apache.tika.parser.Parser;
32  import org.xml.sax.ContentHandler;
33  import org.xml.sax.SAXException;
34  
35  /**
36   * PDF parser.
37   * <p>
38   * This parser can process also encrypted PDF documents if the required
39   * password is given as a part of the input metadata associated with a
40   * document. If no password is given, then this parser will try decrypting
41   * the document using the empty password that's often used with PDFs.
42   */
43  public class PDFParser implements Parser {
44  
45      /**
46       * Metadata key for giving the document password to the parser.
47       *
48       * @since Apache Tika 0.5
49       */
50      public static final String PASSWORD = "org.apache.tika.parser.pdf.password";
51  
52      private static final Set<MediaType> SUPPORTED_TYPES =
53          Collections.singleton(MediaType.application("pdf"));
54  
55      public Set<MediaType> getSupportedTypes(ParseContext context) {
56          return SUPPORTED_TYPES;
57      }
58  
59      public void parse(
60              InputStream stream, ContentHandler handler,
61              Metadata metadata, ParseContext context)
62              throws IOException, SAXException, TikaException {
63          PDDocument pdfDocument = PDDocument.load(stream);
64          try {
65              if (pdfDocument.isEncrypted()
66                      && !pdfDocument.getCurrentAccessPermission().canExtractContent()) {
67                  try {
68                      String password = metadata.get(PASSWORD);
69                      if (password == null) {
70                          password = "";
71                      }
72                      pdfDocument.decrypt(password);
73                  } catch (Exception e) {
74                      // Ignore
75                  }
76              }
77              metadata.set(Metadata.CONTENT_TYPE, "application/pdf");
78              extractMetadata(pdfDocument, metadata);
79              PDF2XHTML.process(pdfDocument, handler, metadata);
80          } finally {
81              pdfDocument.close();
82          }
83      }
84  
85      /**
86       * @deprecated This method will be removed in Apache Tika 1.0.
87       */
88      public void parse(
89              InputStream stream, ContentHandler handler, Metadata metadata)
90              throws IOException, SAXException, TikaException {
91          parse(stream, handler, metadata, new ParseContext());
92      }
93  
94      private void extractMetadata(PDDocument document, Metadata metadata)
95              throws TikaException {
96          PDDocumentInformation info = document.getDocumentInformation();
97          addMetadata(metadata, Metadata.TITLE, info.getTitle());
98          addMetadata(metadata, Metadata.AUTHOR, info.getAuthor());
99          addMetadata(metadata, Metadata.CREATOR, info.getCreator());
100         addMetadata(metadata, Metadata.KEYWORDS, info.getKeywords());
101         addMetadata(metadata, "producer", info.getProducer());
102         addMetadata(metadata, Metadata.SUBJECT, info.getSubject());
103         addMetadata(metadata, "trapped", info.getTrapped());
104         try {
105             addMetadata(metadata, "created", info.getCreationDate());
106         } catch (IOException e) {
107             // Invalid date format, just ignore
108         }
109         try {
110             Calendar modified = info.getModificationDate(); 
111             addMetadata(metadata, Metadata.LAST_MODIFIED, modified);
112         } catch (IOException e) {
113             // Invalid date format, just ignore
114         }
115     }
116 
117     private void addMetadata(Metadata metadata, String name, String value) {
118         if (value != null) {
119             metadata.add(name, value);
120         }
121     }
122 
123     private void addMetadata(Metadata metadata, String name, Calendar value) {
124         if (value != null) {
125             metadata.set(name, value.getTime().toString());
126         }
127     }
128 
129 }