3

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.

Marcel Hofgesang
  • 951
  • 1
  • 15
  • 36
DNB5brims
  • 29,344
  • 50
  • 131
  • 195

7 Answers7

4

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
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
4

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.

Guru
  • 4,693
  • 3
  • 25
  • 45
1

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: ""))
pableiros
  • 14,932
  • 12
  • 99
  • 105
1

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.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
0

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.

Marcel Hofgesang
  • 951
  • 1
  • 15
  • 36
0

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).

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • But I got the binary data, instead of the .pdf file on the website. – DNB5brims Nov 15 '11 at 08:21
  • What do you mean with "binary data"? – DarkDust Nov 15 '11 at 08:23
  • Something like this:JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nM1YS48cNRBWCJDQGyWB8A4PH7uR1riq/LwiEBK3RCNx2HAKJAHtBO3yW/i/fG73uN3LzGQYEWWzh9iecj2+r6ps94UymliZ/LcZPF133z4O6vlf3YUijm5ctOSUN6RT8sq5INqHIhjV9392j7KoC2EUdUYUJ4mKXaAql/bIGR+rHJndguS8mwVpi6CZBE0ryEVwDFI9/nEaXD7vLrqoJf8bF9rx07X6bpX3WujSwTqvVs86g/CTFRpFSAVAlpJTgZ1OntRq3fU3htUf3Q8rmCOV/2CF1O+dxKQtoLPJOk1RWXH4NcURz8vfumffNF7lXwsVjIAkFilstXNMrsRUjVQD4XgD4SAD7ngD7iADcrwBOcgAHW – DNB5brims Nov 15 '11 at 08:26
  • This looks like it's Base64 encoded. See [Any base64 library on iphone-sdk?](http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk) on how to decode it. – DarkDust Nov 15 '11 at 08:29
0

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.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42