I receive a binary pdf file, which is base64Binary encoded. How can I convert it back on the iPhone? What tool kits can I use?
Thanks.
I receive a binary pdf file, which is base64Binary encoded. How can I convert it back on the iPhone? What tool kits can I use?
Thanks.
Solved:
NSData theData = [NSData dataWithData:[GTMBase64 decodeString:theBinary]]; //first transfer it to NSData.
[m_oTestingWeb loadData:theData
MIMEType:@"application/pdf"
textEncodingName:@"UTF-8"
baseURL:nil]; //using the web view to show it back
Solution
NSString *binaryString = [myDict objectForKey:@"key"];
NSData* myData = [NSData dataFromBase64String: binaryString];
NSLog(@" %@",datas);
[self savePDF:myData];
and save PDF is like this
- (void)savePDF:(NSData *)pdfContent
{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"];
NSLog(@"%@",finalPath);
NSURL *url = [NSURL fileURLWithPath:finalPath];
[pdfContent writeToURL:url atomically:YES];
// [aWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Get headers here.
To convert a binary (base64 pdf string) to pdf and show it you can do this for Swift 3:
// Here you convert the binary base 64 pdf string to pdf
let pdfData = Data(base64Encoded: theBase64PDFString, options: .ignoreUnknownCharacters)
// here you can display the pdf on a UIWebView
webView.load(pdfData!,
mimeType: "application/pdf",
textEncodingName: "UTF-8",
baseURL: URL(fileURLWithPath: ""))
If you mean NSData by the word "binary" then you can follow the following method.
I think if the binary data is already in PDF format then we can just use the normal NSData
method to writeToFile:atomically
to write it to the file.
[<NSDataObject> writeToFile:<FilePath> atomically:YES];
And then you can use the DocInteraction to open and display a PDF File.
Hope this helps you.
To convert the binary into a PDF file
@IBOutlet var pdfView: PDFView!
guard
var fileURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last,
let convertedData = Data(base64Encoded: data.base64EncodedData()) // data is the binary pdf
else {
// handle error when getting documents URL
return
}
fileURL.appendPathComponent("filename.pdf")
do {
try convertedData.write(to: documentsURL)
} catch {
// handle write error
}
Bonus: View the PDF without deprecated UIWebView API
let pdfView = PDFView(frame: self.view.bounds)
self.view.addSubview(pdfView)
// Fit content in PDFView.
pdfView.autoScales = true
pdfView.document = PDFDocument(url: fileURL)
Maybe it's a bit late, but I hope it will help someone else at least.
To display a PDF, you can simply use a UIWebView
and feed it the PDF. If you want it a little more sophisticated, look at Apple's ZoomingPDFViewer sample code (found that one in an answer to another question).
Visit this site base64 encoding. Here You will get code for decoding base64 data to NSData. Using that data you can save pdf, view pdf using UIWebView or using Quartz Core.