Моя цель - преобразовать PDF в xhtml, чтобы встроенные изображения могли быть правильно связаны в html.Извлечь встроенные TIFF в TIKA 1.12 vs TIKA 1.14
Приведенный ниже код работает в TIKA 1.12, но не 1.14. Похоже, проблема заключается в том, что в 1.12 использовался PDFBox 1.8, а в 1.14 используется PDFBox 2.0.
В 1.14 я получаю ошибки шрифта и ошибки tiff, например.
WARN org.apache.pdfbox.pdmodel.font.PDSimpleFont - No Unicode mapping for g74 (103) in font HFKECA+TimesNewRoman
ERROR org.apache.pdfbox.tools.imageio.ImageIOUtil - No ImageWriter found for 'tif' format
Из различных форумах и глядя на источник ТИКА казалось, что мне нужно, чтобы включить jai_imageio.jar
на моем пути для ссоры. Это не остановило ошибки. Я также попытался добавить следующее с ним и замены его с jai-imageio-core-1.3.1.jar
(The GitHub версия):
jempbox-1.8.13.jar
fontbox-2.0.4.jar
levigo-jbig2-imageio-1.6.5.jar
Опять же, ни один из этих банок, казалось, не делать ничего. Однако использование банкомата TIKA 1.12 само по себе дает отличные результаты.
Что нужно сделать, чтобы сделать TIKA 1.14 без этих предупреждений (как это делает TIKA 1.12)?
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tika.example;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.nio.charset.Charset;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.exception.TikaException;
import org.apache.tika.extractor.EmbeddedDocumentExtractor;
import org.apache.tika.extractor.ParsingEmbeddedDocumentExtractor;
import org.apache.tika.io.FilenameUtils;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.tika.sax.ToXMLContentHandler;
import org.apache.tika.parser.pdf.PDFParserConfig;
public class ExtractEmbeddedFiles {
private Parser parser = new AutoDetectParser();
private Detector detector = ((AutoDetectParser)parser).getDetector();
private TikaConfig config = TikaConfig.getDefaultConfig();
public void extract(String inputPath) throws SAXException, TikaException, IOException {
File inputFile = new File(inputPath);
String parentDirectory = inputFile.getAbsoluteFile().getParentFile().getPath();
InputStream inputStream = new FileInputStream(inputFile);
File outputDirectory = new File(parentDirectory, inputFile.getName() + "-extracted");
Parser parser = new AutoDetectParser();
ToXMLContentHandler handler = new org.apache.tika.sax.ToXMLContentHandler();
PDFParserConfig pdfConfig = new PDFParserConfig();
pdfConfig.setExtractInlineImages(true);
ParseContext parseContext = new ParseContext();
parseContext.set(PDFParserConfig.class, pdfConfig);
parseContext.set(Parser.class, parser);
EmbeddedDocumentExtractor ex = new MyEmbeddedDocumentExtractor(outputDirectory.toPath(), parseContext);
parseContext.set(EmbeddedDocumentExtractor.class, ex);
Metadata metadata = new Metadata();
parser.parse(inputStream, handler, metadata, parseContext);
String text = handler.toString().trim();
File outputFile = new File(outputDirectory, inputFile.getName() + ".xhtml");
PrintWriter printer = new PrintWriter(new BufferedWriter (new OutputStreamWriter(
new FileOutputStream(outputFile.getPath() ),
Charset.forName("UTF-8").newEncoder()
)));
printer.print(text);
printer.close();
}
private class MyEmbeddedDocumentExtractor extends ParsingEmbeddedDocumentExtractor {
private final Path outputDir;
private int fileCount = 0;
private MyEmbeddedDocumentExtractor(Path outputDir, ParseContext context) {
super(context);
this.outputDir = outputDir;
}
@Override
public boolean shouldParseEmbedded(Metadata metadata) {
return true;
}
@Override
public void parseEmbedded(InputStream stream, ContentHandler handler, Metadata metadata, boolean outputHtml)
throws SAXException, IOException {
//try to get the name of the embedded file from the metadata
String name = metadata.get(Metadata.RESOURCE_NAME_KEY);
if (name == null) {
name = "file_" + fileCount++;
} else {
//make sure to select only the file name (not any directory paths
//that might be included in the name) and make sure
//to normalize the name
name = FilenameUtils.normalize(FilenameUtils.getName(name));
}
//now try to figure out the right extension for the embedded file
MediaType contentType = detector.detect(stream, metadata);
if (name.indexOf('.')==-1 && contentType!=null) {
try {
name += config.getMimeRepository().forName(
contentType.toString()).getExtension();
} catch (MimeTypeException e) {
e.printStackTrace();
}
}
//should add check to make sure that you aren't overwriting a file
Path outputFile = outputDir.resolve(name);
//do a better job than this of checking
Files.createDirectories(outputFile.getParent());
Files.copy(stream, outputFile);
}
}
}
запустите 'System.out.println (Arrayys.toString (ImageIO.getReaderFormatNames()));' с самого начала, что вы получаете? –
с TIKA 1.12 и 1.14 Я получаю '[JPEG 2000, JPG, JPG, JPEG2000, TIFF, BMP, BMP, GIF, GIF, WBMP, PNG, PNG, raw, RAW, JPEG, pnm, PNM, tif, TIF, TIFF, jpeg2000, wbmp, jpeg, jpeg 2000] ' –
Следующее, что нужно попробовать: 1)' System.out.println (ImageIO.getImageWritersByFormatName ("tif"). Next()); '2) создать BufferedImage, а затем вызвать 'ImageIOUtil.write (bim," tif ", новый FileOutputStream (....), 72,1);' это создает файл? Если это не дает подсказки, я бы попробовал ваш код ... это происходит с любым PDF-файлом или только с некоторыми? –