我有一个iOS应用程序,在这个应用程序中我想从.djvu文件中查看数据。是否有任何方法来读取.djvu文件在Swift或在内部UIWebview。
我还尝试了以下解决方案来查看uiwebview中的djvu文件,但这并没有帮助。
1.在uiwebview中直接打开djvu文件
let urlPage : URL! = URL(string: "http://192.168.13.5:13/myData/5451890-OP.djvu")
webView.loadRequest(URLRequest(url: urlPage))2.第二,我尝试将djvu文件转换成pdf,并显示转换后的pdf文件为视图。参考链接:https://github.com/MadhuriMane/ios-djvu-reader,但这会提供低质量的镜像。
3.我尝试在UIDocumentInteractionController()及其委托方法的帮助下预览文件,但没有工作。
请提出任何可能的办法。
发布于 2018-02-24 12:18:37
请记住,与类似的格式(如EPUB、MOBI、PDF和其他eBook文件格式)相比,eBook文件不那么受欢迎,因此我有以下解决问题的方法。
1)创建一个Web,将djvu文件转换为pdf ex.:http://example.com/djvuToPdf/djvuFile/outputFile。
2)在UIWebView中读取PDF文件
要创建Web服务,我假设您可以访问任何,在我的例子中是Ubuntu16.04。
第一步:安装djvulibre sudo apt-get install djvulibre-bin ghostscript
第二步:测试运行$ djvups inputFile.djvu | ps2pdf - outputFile.pdf。您还可以使用ddjvu命令。但是,使用ddjvu命令转换的文件比djvups命令大10倍。您应该考虑使用--help --探索诸如mode、quality等设置。
第三步:创建一个Web服务(为了保持简单,我使用PHP,在方便的时候使用任何东西Python )
<?php
$inputFile = $_GET['input_file'];
$outputFile = $_GET['output_file'];
// use shell exec to execute the command
// keep in mind that the conversion takes quite a long time
shell_exec(sprintf("djvups %s | ps2pdf - %s", $inputFile, $outputFile));
$name = $outputFile;
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;
?>最后一步:在应用程序中加载PDF
按照苹果的建议,考虑使用WKWebView代替UIWebView。
if let pdfURL = Bundle.main.url(forResource: "pdfFile", withExtension: "pdf", subdirectory: nil, localization: nil) {
do {
let data = try Data(contentsOf: pdfURL)
let webView = WKWebView(frame: CGRect(x:20,y:20,width:view.frame.size.width-40, height:view.frame.size.height-40))
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
view.addSubview(webView)
}
catch {
// catch errors here
}
}https://stackoverflow.com/questions/47467697
复制相似问题