我有iOS应用程序,我想在其中查看.djvu
文件的数据。有什么方法可以在Swift或UIWebview
内读取.djvu
文件。
我还尝试了遵循解决方案在uiwebview中查看DJVU文件,但这无济于事。
1。直接打开uiwebview
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()
的帮助下预览文件,并且是委托方法,但不起作用。
请建议这样做的任何可能的方法。
请记住,.djvu文件不及Epub,Mobi,PDF和其他电子书文件格式的类似格式,我将采用以下方法来解决问题。
1)创建一个Web服务,将DJVU文件转换为PDF例如:http://example.com/djvuToPdf/djvuFile/outputFile
2)读取UIWebView
中的PDF文件
要创建一个Web服务,我假设您可以访问任何Linux分布式服务器,在我的情况下,Ubuntu 16.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 Golang]时使用任何内容)
<?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
正如Apple所建议的,请考虑使用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
}
}