我正在使用HTML文件在DocumentDirectory中生成 pdf文件。每次调用以下方法时,我需要刷新(使用在 html 文件中具有相同名称的新图像) HTML 文件。
现在我正在这样做,但它只是第一次令人耳目一新(当我调用这个方法时)
-(void)RefreshingHTML
{
// fetching path
NSArray* deletepath_forDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* deletedocumentsDirectoryfiles = [deletepath_forDirectory objectAtIndex:0];`
// Delete HTML file in DocumentDirectory
NSString *deleteHTMLPath = [deletedocumentsDirectoryfiles stringByAppendingPathComponent:@"HTML_Demo.html"];
NSString *deletePDFPath = [deletedocumentsDirectoryfiles stringByAppendingPathComponent:@"HTML_Demo.pdf"];
NSLog(@"delete HTML file Path : %@",deleteHTMLPath);
if([[NSFileManager defaultManager] fileExistsAtPath:deleteHTMLPath]) {
[[NSFileManager defaultManager] removeItemAtPath:deleteHTMLPath error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:deletePDFPath error:NULL];
}
// fetching HTML file from supporting files
NSString *path = [[NSBundle mainBundle] pathForResource:@"HTML_Demo" ofType:@"html"];
NSURL *pathURL = [NSURL fileURLWithPath:path];
NSArray* path_forDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* documentsDirectory = [path_forDirectory objectAtIndex:0];
// saving HTML file to DocumentDirectory
NSData* data = [NSData dataWithContentsOfURL:pathURL];
[data writeToFile:[NSString stringWithFormat:@"%@/HTML_Demo.html",documentsDirectory] atomically:YES];
// Fetching HTML file from DocumentDirectory
NSString *HTMLPath = [documentsDirectory stringByAppendingPathComponent:@"HTML_Demo.html"];
NSLog(@"delete HTML file Path: %@",HTMLPath);
if([[NSFileManager defaultManager] fileExistsAtPath:HTMLPath]) {
NSURL *targetURL = [NSURL fileURLWithPath:HTMLPath];
// Converting HTML to PDF
self.PDFCreator = [NDHTMLtoPDF createPDFWithURL:targetURL
pathForPDF:[@"~/Documents/HTML_Demo.pdf" stringByExpandingTildeInPath]
delegate:self
pageSize:kPaperSizeA4
margins:UIEdgeInsetsMake(20, 5, 90, 5)];
}
}