Here I am using Reference swift project to load a PDF file in PDFViewer in WKWebview.
Reference Project - https://github.com/Minitex/PDFJSTest/blob/master/PDFJSTest/ViewController.swift
Here in the reference project have a code look like this for load PDF file
func openPDFInViewer(myURL: URL)
{
let pdf = NSData(contentsOf: myURL)
let length = pdf?.length
var myArray = [UInt8](repeating: 0, count: length!)
pdf?.getBytes(&myArray, length: length!)
webView?.evaluateJavaScript("PDFViewerApplication.open(new Uint8Array(\(myArray)))", completionHandler: { result, error in
print("Completed Javascript evaluation.")
print("Result: \(String(describing: result))")
print("Error: \(String(describing: error))")
})
}
And I tried to convert this function into Objective-C language and it looks like this:
-(void)renderPDF:(NSTimer*)theTimer
{
NSMutableDictionary *dictData = [[NSMutableDictionary alloc] initWithDictionary:theTimer.userInfo];
NSURL *requestURL = (NSURL *)[dictData valueForKey:@"request_url"];
NSData *data = [NSData dataWithContentsOfURL:requestURL];
NSUInteger len = [data length];
uint8_t myArray[len];
[data getBytes:&myArray length:len];
NSString *strForEvaluate = [NSString stringWithFormat:@"PDFViewerApplication.open(new Uint8Array('%s'));",myArray];
[wkWebView evaluateJavaScript:strForEvaluate completionHandler:^(id Result, NSError * _Nullable error) {
if (error)
{
NSLog(@"This is error....%@",error.description);
}
else if(Result)
{
NSLog(@"%@",Result);
}
}];
}
When I tried to run the app and evaluateJavaScript function that time getting error response like this
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=SyntaxError: Unexpected EOF, WKJavaScriptExceptionColumnNumber=0, WKJavaScriptExceptionSourceURL=file:///Users/sapanaranipa/Library/Developer/CoreSimulator/Devices/207FA841-3256-45D2-8698-2B769A72A2F4/data/Containers/Bundle/Application/4BD7E620-DF16-429B-BBC2-2A36BA2A2DE8/SureFox.app//PDF_Web/pdf.js-dist/web/viewer.html, NSLocalizedDescription=A JavaScript exception occurred}
Please help me, Thanks in Advance.
You treat an arbitrary byte array as a C-string and that might not be null-terminated or contain stuff like '
which you don't want in your JavaScript. So it may as well crash when you try to create your string.
The problematic part is using the %s
formatter here:
NSString *strForEvaluate = [NSString stringWithFormat:@"PDFViewerApplication.open(new Uint8Array('%s'));",myArray];
(Also, uint8_t myArray[len];
may cause a stack overflow [ha!] for large data.)
What you want instead is to create a string that represents a JavaScript array of numbers, like [25, 243, 0, 123]
. Here's a way that might work (untested):
NSMutableArray<NSString *> *bytes = [NSMutableArray array];
const uint8_t *rawBytes = data.bytes;
for (NSUInteger i = 0; i < data.length; i++) {
[bytes addObject:[NSString stringWithFormat:@"%d", (int)rawBytes[i]]];
}
NSString *javaScriptArray = [bytes componentsJoinedByString:@","];
NSString *strForEvaluate = [NSString stringWithFormat:
@"PDFViewerApplication.open(new Uint8Array([%@]));", javaScriptArray];