如何在 UIWebview 中查看 DJVU 文件

How to view DJVU file in UIWebview

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

我也尝试过以下解决方案在 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 文件显示为视图。 参考文献link: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 探索 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;


?>

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

}