UIIMagePickerController & NSURLConnection - objective-c

UIImagePickerController freezes while I think the Image is being compressed or being sent by NSURLConnection although I'm not convinced it's the later.
What I want is, all this to be done in the background. Instead when I pick a photo from the library, the Screen freezes for what feels like forever. This isn't optimal, obviously. What approach should I take here?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
int i = 0;
NSString *uniquePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/selectedImage.png"];
while ([[NSFileManager defaultManager] fileExistsAtPath:uniquePath])
{
uniquePath = [NSString stringWithFormat:#"%#/%#-%d.%#", [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"], #"selectedImage", ++i, #"png"];
}
NSLog(#"Writing selected Image to Documents Folder, %#", uniquePath);
dataForPNGFile = UIImagePNGRepresentation(img);
if (!dataForPNGFile) return NO;
[UIImagePNGRepresentation(img) writeToFile:uniquePath atomically:YES];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
[[self parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
NSString *urlString = #"http://localhost:3000/photos";
NSURL *url = [NSURL URLWithString:urlString];
NSString *boundary = #"----1010101010";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSString *photoPath = [[NSBundle mainBundle] pathForResource:#" my-photo " ofType:#"jpg"];
NSData *photoData = [NSData dataWithContentsOfFile:photoPath];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"photo-description\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"testing 123" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"photo-file\"; filename=\"%#\"\r\n", " my-photo.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:photoData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"tags\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"random,test,example" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:#"%d", body.length] forHTTPHeaderField: #"Content-Length"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

You should use:
detachNewThreadSelector:toTarget:withObject:
Detaches a new thread and uses the specified selector as the thread entry point.
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
Documentation here: NSThread
This will create a new thread for the operation that is eating up the cpu, so your UI isn't affected remember to create a NSAutoreleasePool for the new thread, as only the main thread gets a NSAutoreleasePool pool by default.
So at the beginning of your selector do:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//and at the end:
[pool drain]:

Related

- connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: never call

I want to notify my user about the progress of the upload of some Mpeg 3 files. Just one.
but I get issue as I say in the title.
My upload code is here
- (NSString *) upload: (NSData*) postData {
NSString *str;
NSURL *url = [NSURL URLWithString:#"http://example.com/test.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
request.HTTPMethod = #"POST";
NSString *boundary = #"unique-consistent-string";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add mp3 data
if (postData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#; filename=audio\r\n", #"audioFormKey"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: audio/mpeg3\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:postData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength2 = [NSString stringWithFormat:#"%lu", (unsigned long)[body length]];
[request setValue:postLength2 forHTTPHeaderField:#"Content-Length"];
NSLog(postLength2);
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(data.length > 0)
{
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(str);
}
}];
return str;
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
NSLog(#"bytes transfer %li" ,(long)bytesWritten);
}
Here the Log "bytes transfer" is never call.
Thanks a lot for support
A bit odd to be calling sendAsynchronousRequest: and then sendSynchronousRequest: in the completion handler?
In any case, you aren't configuring the URL connection to have a delegate and, thus, your delegate method is never called.
You'll need to allocated and instantiate the URLConnection and then configure it to use your object as the delegate. And it needs to conform to the NSURLConnectionDelegate protocol.

Printing to Google Cloud Print using iOS

I have been trying to print a PDF file to Google cloud print using the following code. Instead of actually printing the document, it is only printing a string that shows the location of the file as well as the file name. I would like to have it print the actual document.
We can assume that the data being passed into this method is valid as it seems to be finding the correct printer from calls to cloud prints /search
Update: The code is updated from its original format to include some multi-part form data
NSString *currentPDFFileName = [[NSBundle mainBundle] pathForResource:#"TestPDF" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];
NSData *validPDF = [[NSString stringWithString:#"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF]))
{
NSLog (#"Not valid");
}
NSLog (#"%#", currentPDFFileName);
NSLog (#"%#", [currentPDFFileName lastPathComponent]);
//create the body
NSMutableData *body = [NSMutableData data];
//create the POST vars
NSString *thePrinter = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%#", printerID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"content\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
NSString *theContent = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"contentType\"\r\n\r\n%#", contentType];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[thePrinter dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[theContent dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://www.google.com/cloudprint/submit"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
[self.authenticate authorizeRequest:pdfRequest completionHandler:^(NSError *error) {
NSString *output = nil;
if(error) {
output = [error description];
} else {
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:pdfRequest
returningResponse:&response
error:&error];
if(data) {
output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
output = [error description];
}
}
NSLog(#"%#", output);
}];

Best practice when using similar code in methods

I have a Utility method to comunicate with an API, it communicates using a POST HTTP request, in my utility-class i have a method called:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
wich takes the parameters and sets up the body of the POST. however, in one particular case i want to upload some images and the code is slightly altered to make uploading of said images possible, what is the best practice for this case? should i rename the method to:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;
and set nil as a parameter in all other cases, or should i set a bool in the method calling "makeConnectionWithParameters" and check in the method if the bool is set and in that case process the images?
Any other ideas to make the code prettier?
here is the method:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:BASE_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20];
request.HTTPMethod = #"POST";
NSString *boundary = #"myR4ND0Mboundary";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
//Lägg till inloggningsuppgifter för API-anropet
[parameters setValue:API_LOGIN forKey:#"api-login"];
[parameters setValue:API_PASSWORD forKey:#"api-password"];
//Lägg till alla parameterar i POST-bodyn
NSMutableData *body = [NSMutableData data];
for (NSString *param in parameters)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if(hasImages)
{
int c = 0;
for(UIImage* image in self.images)
{
c++;
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if (imageData)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", [NSString stringWithFormat:#"image%d", c]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
imageData = nil;
}
}
//Sätt content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:body];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection)
{
receivedData = [NSMutableData dataWithLength:0];
}
}
I hope this all makes sense. :)
Simply create two methods that make the request objects, not the actual connections:
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;
Inside the first method call:
[self makeConnectionWithParameters:parameters andImages:nil];
...and have a nil check for the images inside the second method. Use those two methods to get your request then make the NSURLConnection with the returned object.

How to upload multiple images without using ASIHTTPRequest

I want to upload two images to the server with following code can any one please help me, here is my code for post data
- (void)uploadPath:(NSString*)path withOptions:(NSDictionary*)options withImageData1:(NSData*)data1 ofImageName1:(NSString *)imageName1 andImageData2:(NSData*)data2 ofImageName2:(NSString *)imageName2 {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:baseURLString]];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.0];
NSString *boundary = #"-------1234567";
[mutableRequest setValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary] forHTTPHeaderField:#"Content-Type"];
[mutableRequest setHTTPMethod:#"POST"];
NSMutableData *postbody = [NSMutableData data];
for(NSString *key in options)
{
NSString *value = [options objectForKey:key];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
if ([imageName1 isEqualToString:#"picture1.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"picture1\"; filename=\"%#\"\r\n",imageName1] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data1]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
//******************* Append 2nd Image ************************/
if ([imageName2 isEqualToString:#"picture2.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"picture2\"; filename=\"%#\"\r\n",imageName2] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data2]];
//******************* Append 2nd Image ************************/
[mutableRequest setHTTPBody:postbody];
urlConnection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
if (self.urlConnection) {
self.dataXml = [NSMutableData data];
}
self.urlConnection = nil;
[mutableRequest release];
[pool release];
}
One solution to see what is wrong:
make a new project, using ASIHttpRequest
Get a web debugging proxy like Charles
Turn on Charles (or whatever), do a multi-file upload that succeeds.
Record data from HTTP traffic to server.
Now run your own code. Record that traffic. What is different from
your traffic to the working traffic? Makes yours IDENTICAL in all
respects, and how can it not work?
You can also replace step (1) with a curl command line that uploads multiple files to a server and record that traffic, but I'm not versed enough in curl to say off the top of my head what that command would look like.
- (NSString*) nameValString: (NSDictionary*) dict {//example CHLOVA_FORM_BOUNDARY is #"aaa"
NSArray* keys = [dict allKeys];
NSString* result = [NSString string];
int i;
for (i = 0; i < [keys count]; i++) {
result = [result stringByAppendingString:
[#"--" stringByAppendingString:
[CHLOVA_FORM_BOUNDARY stringByAppendingString:
[#"\r\nContent-Disposition: form-data; name=\"" stringByAppendingString:
[[keys objectAtIndex: i] stringByAppendingString:
[#"\"\r\n\r\n" stringByAppendingString:
[[dict valueForKey: [keys objectAtIndex: i]] stringByAppendingString: #"\r\n"]]]]]]];
}
return result;
}
- (void)sendPhoto{
NSString *param = [self nameValString:dic];//dic is post info like {lat=36;lng=120;}
NSString *footer = [NSString stringWithFormat:#"\r\n--%#--\r\n", CHLOVA_FORM_BOUNDARY];
param = [param stringByAppendingString:[NSString stringWithFormat:#"--%#\r\n", CHLOVA_FORM_BOUNDARY]];
param = [param stringByAppendingString:#"Content-Disposition: form-data; name=\"pic\";filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"];
NSData *jpeg = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:_path], 0.55);
//NSLog(#"jpeg size: %d", [jpeg length]);
NSMutableData *data = [NSMutableData data];
//img one
[data appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg];
[data appendData:[footer dataUsingEncoding:NSUTF8StringEncoding]];
//img two
[data appendData:[param2 dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg2];
[data appendData:[footer2 dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", CHLOVA_FORM_BOUNDARY];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:#"POST"];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [data length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:data];
//then do sending
}

Failing to POST NSData of PDF to server

I have an iPad application in which I need to send a server a PDF file, alongside two POST variables, a user ID and a job ID. I am using the code below to do this:
NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];
NSData *validPDF = [[NSString stringWithString:#"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF]))
{
NSLog (#"Not valid");
}
NSLog (#"%#", currentPDFFileName);
NSLog (#"%#", [currentPDFFileName lastPathComponent]);
//create the body
NSMutableData *body = [NSMutableData data];
//create the POST vars
NSString *jobID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"jobid\"\r\n\r\n%#", job.jobID];
NSString *userID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%#", delegate.userID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image_file\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[jobID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[userID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
NSData *pdfDataReply;
NSURLResponse *pdfResponse;
NSError *pdfError;
pdfDataReply = [NSURLConnection sendSynchronousRequest:pdfRequest returningResponse:&pdfResponse error:&pdfError];
NSString *pdfResult = [[NSString alloc] initWithData:pdfDataReply encoding:NSASCIIStringEncoding];
NSLog (#"%#", pdfResult);
The people on the other end of the connection are saying that they are not receiving the PDF file. I have tried all the usual suspects such as trying to find any nil objects.
Would greatly appreciate if anyone could please lend a hand with this!
Thanks.
Ricky.
Turns out there was an issue with the boundaries. I needed to add one at the end, after pdfData was added. Thank you for your help.