首先使用NDHTMLtoPDF将 DOC 转换为 PDF 。
下面的委托方法将被调用
- (void)HTMLtoPDFDidSucceed:(NDHTMLtoPDF*)htmlToPDF
{
NSLog(@"HTMLtoPDF did succeed (%@ / %@)", htmlToPDF, htmlToPDF.PDFpath);
NSURL *pptURL = [NSURL fileURLWithPath:htmlToPDF.PDFpath];
NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
[myWebView loadRequest:request];
//now merge whole pages to one PDF
[self MergeToOnePagePDF:pptURL];
}
现在添加这些方法:
-(void)MergeToOnePagePDF:(NSURL *)pdfURL
{
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL);
int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdf, 1);
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
float pageHeight = pageRect.size.height;
pageRect.size.height = pageRect.size.height * pageCount;
NSMutableData* pdfData = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)pdfData);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &pageRect, NULL);
CGPDFContextBeginPage(pdfContext, NULL);
CGContextTranslateCTM(pdfContext, 0, pageRect.size.height);
for (int i = 1; i <= pageCount; i++)
{
pageRef = CGPDFDocumentGetPage(pdf, i);
CGContextTranslateCTM(pdfContext, 0, -pageHeight);
CGContextDrawPDFPage(pdfContext, pageRef);
}
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"destination.pdf"];
[pdfData writeToFile: pdfFile atomically: NO];
}
编辑:将页面合并为一页 PDF 信用转到@iPDFDev