如何在UIWebview中查看DJVU文件

Sur*_*ane 8 uiwebview djvu ios swift3

我正在使用iOS应用程序,我想在其中查看.djvu文件中的数据.有没有办法.djvu在Swift或里面读取文件UIWebview.

我也尝试过以下解决方案来查看uiwebview中的djvu文件,但这没有帮助.

1.直接打开djvu文件uiwebview

 let urlPage : URL! = URL(string: "http://192.168.13.5:13/myData/5451890-OP.djvu")        
 webView.loadRequest(URLRequest(url: urlPage))
Run Code Online (Sandbox Code Playgroud)

2.其次,我试图将djvu文件转换为pdf,并将转换后的pdf文件转换为视图.参考链接:https://github.com/MadhuriMane/ios-djvu-reader 但这样可以提供低质量的镜像.

3.我试图借助于UIDocumentInteractionController()它的委托方法预览文件但是没有用.

请建议任何可行的方法.

Odd*_*Odd 3

请记住,.djvu 文件不如 EPUB、MOBI、PDF 和其他电子书文件格式等类似格式流行,我将采用以下方法来解决该问题。

1) 创建一个 Web 服务将 djvu 文件转换为 pdf 例如:http://example.com/djvuToPdf/djvuFile/outputFile

2)读取PDF文件UIWebView

要创建 Web 服务,我假设您可以访问任何 Linux 分布式服务器,在我的例子中是 Ubuntu 16.04。

第一步:安装 djvulibre sudo apt-get install djvulibre-bin ghostscript

第二步:试运行$ djvups inputFile.djvu | ps2pdf - outputFile.pdf。您也可以使用该ddjvu命令。但是,使用ddjvu命令转换的文件比djvups命令大 10 倍。您可能需要考虑使用“探索”等--help设置。modequality

第三步:创建一个 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;


?>
Run Code Online (Sandbox Code Playgroud)

最后一步:在应用程序中加载 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
    }

}
Run Code Online (Sandbox Code Playgroud)