0

我想知道如何从 HTML 页面获取 PDF。

我正在使用该方法

UIGraphicsBeginPDFContextToFile(self.filePath, CGRectZero, nil);    //creating PDF

    int i = 0;
    for (; i < pages; i++)
    {
        if (pageHeight * (i+1) > height)
        {
            CGRect f = [myWebPage frame];
            f.size.height -= (((i+1) * pageHeight) - height);
            [myWebPage setFrame: f];
        }
        // Specify the size of the pdf page
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, width, pageHeight), nil);
        CGContextRef currentContext =  UIGraphicsGetCurrentContext();
        [[[myWebPage subviews] lastObject] setContentOffset:CGPointMake(0, pageHeight * i) animated:NO];
        [myWebPage.layer renderInContext: currentContext];
        NSLog(@"I = %d",i);
    }

UIGraphicsEndPDFContext();
[[[myWebPage subviews] lastObject] setContentOffset:CGPointMake(0, 0) animated:NO];
[myWebPage setFrame:origframe];

但这不会给出正确的 PDF,因为多页 PDF 的某些部分被剪裁,如果需要超过 10 秒,它会使应用程序崩溃。

我已经下载了https://github.com/iclems/iOS-htmltopdf这个文件并使用了 NDHTMLtoPDF.h 和 NDHTMLtoPDF.m 文件。

它工作正常..

但是当我尝试使用单个功能时

 - (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (webView.isLoading) return;

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];

[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];

CGRect printableRect = CGRectMake(self.pageMargins.left,
                              self.pageMargins.top,
                              self.pageSize.width - self.pageMargins.left - self.pageMargins.right,
                              self.pageSize.height - self.pageMargins.top - self.pageMargins.bottom);

CGRect paperRect = CGRectMake(0, 0, self.pageSize.width, self.pageSize.height);

[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];

NSData *pdfData = [render printToPDF];

[pdfData writeToFile: self.PDFpath  atomically: YES];

[self terminateWebTask];

if (self.delegate && [self.delegate respondsToSelector:@selector(HTMLtoPDFDidSucceed:)])
    [self.delegate HTMLtoPDFDidSucceed:self];


}

- (NSData*) printToPDF
{
NSMutableData *pdfData = [NSMutableData data];

UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );

[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];

CGRect bounds = UIGraphicsGetPDFContextBounds();

for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
    UIGraphicsBeginPDFPage();

    [self drawPageAtIndex: i inRect: bounds];
}

UIGraphicsEndPDFContext();

return pdfData;
}

它显示了错误

NSData *pdfData = [render printToPDF];
[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
[self drawPageAtIndex: i inRect: bounds];

因为它无法定位函数和变量。

但是在代码中,他们没有声明任何变量,例如 numberOfPages 和所有变量,但他们得到了解决方案,但我得到了错误..

任何人都可以向我发送正确的代码和步骤,以将 HTML 文件的打印格式转换为使用 UIPrintPageRenderer 的 PDF。

请帮我..

4

1 回答 1

2

//在这里传递HTML字符串

 NSString *str_PDFName_path = [NSString stringWithFormat:@"~/Library/MyFile.pdf"];
    self.PDFCreator = [NDHTMLtoPDF createPDFWithHTML:str_HTML pathForPDF:[str_PDFName_path stringByExpandingTildeInPath] delegate:self pageSize:kPaperSizeA4 margins:UIEdgeInsetsMake(20, 5, 90, 5)];

//执行此操作后

- (NSData*) printToPDF
{
    NSMutableData *pdfData = [NSMutableData data];
    //595, 842
    UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 20, 595, 842), nil);

   // [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];

    //CGRect bounds = UIGraphicsGetPDFContextBounds();

    for ( int i = 0 ; i < self.numberOfPages ; i++ )
    {
        UIGraphicsBeginPDFPage();

        [self drawPageAtIndex: i inRect: CGRectMake(120, 770, 500, 15)]; //bounds];
        //CGRect textRect = [self addText:@"Centurion Medical Products | 100 Centurion Way | Williamston MI USA 48895 | 800.248.4058"
        //                      withFrame:CGRectMake(120, 770, 500, 15) fontSize:8.0f];
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(currentContext, 99/255.f, 100/255.f, 99/255.f, 1.0f);

        [self addText:@"Medical Products | 100 Centurion Way | Williamston MI USA 48895 | 800.248.4058"
                              withFrame:CGRectMake(130, 770, 500, 15) fontSize:8.0f];
        UIImage *anImage = [UIImage imageNamed:@"logo.gif"];
        //CGRect imageRect = [self addImage:anImage
        //                          atPoint:CGPointMake(50, 768)];
        [self addImage:anImage atPoint:CGPointMake(50, 768)];
    }

    UIGraphicsEndPDFContext();

    return pdfData;
}
于 2013-06-05T07:46:12.977 回答