How to view DJVU file in UIWebview

I have an iOS application in which I want to view data from a .djvu file. Is there a way to read the .djvu file in Swift or inside UIWebview .

I also tried the following solutions to view a djvu file in uiwebview, but this does not help.

1. Directly open the djvu file in UIWebview

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

2. Secondly, I tried to convert the djvu file to pdf and show that the converted PDF file is in sight. Link link: https://github.com/MadhuriMane/ios-djvu-reader But this gives a mirror image with low quality.

3. I tried to view the file using UIDocumentInteractionController() , and it delegates the method, but does not work.

Please suggest any possible ways to do this.

+8
ios swift3 uiwebview djvu
source share
1 answer

Bearing in mind that .djvu files are less popular than similar formats such as EPUB, MOBI, PDF and other e-book formats, I would have the following approach to solving the problem.

1) Create a web service to convert djvu files to pdf for example: http://example.com/djvuToPdf/djvuFile/outputFile

2) Read the PDF file in UIWebView

To create a web service, I would assume that you have access to any distributed Linux server, in my case Ubuntu 16.04.

Step one: install djvulibre sudo apt-get install djvulibre-bin ghostscript

Step Two: Test Run $ djvups inputFile.djvu | ps2pdf - outputFile.pdf $ djvups inputFile.djvu | ps2pdf - outputFile.pdf . You can also use the ddjvu command. However, files converted using the ddjvu command ddjvu 10 times larger than the djvups command. You would like to consider using --help to study settings like mode , quality , etc.

Step three: creating a web service (to keep things simple, I use PHP, use something at your convenience [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 MYH:i:s').' GMT'); echo $content; ?> 

Final step: Download the PDF in the application

As Apple advises, consider using WKWebView instead of 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 } } 
+2
source share

All Articles