How do I convert HTML to PDF on iOS?

Q: How can I convert HTML to PDF on iOS?

A:

This code demonstrates how to convert an HTML file contained within the app bundle to PDF using the sample PDFViewCtrlDemo, included with the SDK.

NSString* pathToHtml = [[NSBundle mainBundle] pathForResource:@“test” ofType:@“html”];

[PTConvert convertOfficeToPDF:pathToHtml completion:^(NSString *pathToPDF) {

@try {

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString* newDoc = [documentsDirectory stringByAppendingPathComponent:@“test.pdf”];

[[NSFileManager defaultManager] copyItemAtPath:pathToPDF toPath:newDoc error:Nil];

docToOpen = [[PTPDFDoc alloc] initWithFilepath:pathToPDF];

[pdfViewCtrl SetDoc:docToOpen];

}

@catch (NSException *e) {

NSLog(@“Could not open document due to exception %@:%@”, e.name, e.reason);

return;

}

}];