Mayur Birari
Mayur Birari

Reputation: 5835

Open NSData (PDF content) in UIWebview which is stored in Sqlite

I want to open pdf file in webview but the scenario is different here, I have saved pdf content as NSData into sqlite table (blob data type), I got success when I was inserting data into sqlite table but once I fetch the blob content into NSData and trying to open that data into WebView it gives me error - failed to find PDF header: `%PDF' not found.
I am using webview's loadData method to load data

[webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
I did lots of search and also gone thru with other stack overflow questions but haven't found sufficient answer.

thanks,
Mayur

Upvotes: 3

Views: 4280

Answers (3)

Sakiboy
Sakiboy

Reputation: 7459

If you are opening up a file to be printed from a webview you want to use the UIPrintViewFormatter's method 'viewPrintFormatter' to assign your webview to the formatter. You would then add your instance of the UIPrintViewFormatter to your instance of the UIPrintInteractionController.

Here's an example of how to use the UIPrintViewFormatter:

 UIViewPrintFormatter *viewPrintFormatter = [self.contentWebView  viewPrintFormatter];
[printInteractionController setPrintFormatter:viewPrintFormatter];

Upvotes: 0

JeremyP
JeremyP

Reputation: 86661

The first four bytes of any PDF file are %PDF. This is telling you that, by the time you are trying to display the data, it is not a PDF file (or if it is, the beginning has been chopped off).

Either you are putting corrupt data into the SQLite database, or you are corrupting it when you get it out. It should be easy to confirm one way or the other by looking at the data in the database using the command line tools.

Upvotes: 1

dcai
dcai

Reputation: 2555

Have you tried to dump the convert NSData to NSString, then dump it to console? PDF file has to start with "%PDF-1.2......", you have to make sure the blob data from sqlite is the full pdf content.

Upvotes: 1

Related Questions