iclems / iOS-htmltopdf

MIT License
305 stars 79 forks source link

Convert multiple HTML to multiple pdf using a for loop. #43

Open Keatooon opened 6 years ago

Keatooon commented 6 years ago

Hi, I'm trying to convert multiple html file to multiple pdf file using a for loop, however, the app crashes with Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).

Why?

Here's the code I'm using:

for (int i=0; i<= 47; i++) {

    NSString *inputHTMLfileName = [NSString stringWithFormat:@"wkhtml_tempfile_%d",j];
    NSString *outputPDFfileName = [NSString stringWithFormat:@"~/Documents/%d_delegateDemo%d.pdf",loop,j];

    NSURL *htmlFileUrl = [[NSBundle mainBundle]
                          URLForResource:inputHTMLfileName withExtension:@"html"];

    // Check for existing pdf file and remove it
    NSError *pdfDeleteError;
    if ([[NSFileManager defaultManager] fileExistsAtPath:outputPDFfileName]){
        //removing file
        if (![[NSFileManager defaultManager] removeItemAtPath:outputPDFfileName error:&pdfDeleteError]){

            NSString * errorMessage = [NSString stringWithFormat:@"wk %d Could not remove old pdf files. Error:%@",j, pdfDeleteError];
            NSLog(@"%@",errorMessage);
        }
    }

    self.PDFCreator = [NDHTMLtoPDF createPDFWithURL:htmlFileUrl pathForPDF:[outputPDFfileName stringByExpandingTildeInPath] pageSize:kPaperSizeA4 margins:UIEdgeInsetsMake(10, 5, 10, 5) successBlock:^(NDHTMLtoPDF *htmlToPDF) {
        NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did succeed (%@ / %@)", htmlToPDF, htmlToPDF.PDFpath];
        NSLog(@"%@",result);
        self.resultLabel.text = result;
    } errorBlock:^(NDHTMLtoPDF *htmlToPDF) {
        NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did fail (%@)", htmlToPDF];
        NSLog(@"%@",result);
        self.resultLabel.text = result;
    }];
}