【问题标题】:Issues with converting HTML to PDF on iOS在 iOS 上将 HTML 转换为 PDF 的问题
【发布时间】:2013-08-01 23:04:36
【问题描述】:

我正在使用以下代码将 HTML 转换为 PDF(由 cwehrung 在各个地方发布)

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];

在 UIPrintPageRenderer 上创建了一个类别来支持:

-(NSData*) printToPDF
{
    [self doNotRasterizeSubviews:self.view];

    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;
}

我遇到的问题是图像和文本之间的对齐问题。

我将 HTML 文件上传到 here,在我上传到 here 的转换后的 PDF 文件中很容易看到问题。 (见第 3 本书和第 6 本书)

任何帮助将不胜感激!

【问题讨论】:

  • 差别不大....我觉得还可以
  • 您可以更改框架pdf。
  • 在 UIGraphicsBeginPDFContextToData 中尝试设置边界 根据 Apple 文档: bound - 一个矩形,指定 PDF 页面的默认大小和位置。 (此值用作每个新页面的默认媒体框。)矩形的原点通常应为 (0, 0)。指定一个空矩形 (CGRectZero) 会将默认页面大小设置为 8.5 x 11 英寸(612 x 792 磅)。尝试将 CGRectZero 更改为另一个值 ..:)
  • 用户,iPhoneNerd,感谢您的回复,但更改 PDF 矩形只会将问题移至其他位置。无论帧大小如何,我都需要一种方法来保持图像和文本始终同步。
  • 你是怎么解决这个问题的?我遇到了类似的问题

标签: html iphone ios pdf


【解决方案1】:

我们可以通过另一种方式将 HTML 转换为 pdf-

在UIWebview中打开html,代码是-

-(void)display
{
webViewHeight = [[self.printWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];

CGRect screenRect = self.printWebView.frame;

double currentWebViewHeight = webViewHeight;


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [documentsDirectory stringByAppendingPathComponent:@"pdfImages"];
if (![[NSFileManager defaultManager] fileExistsAtPath: imageCacheDirPath])
{
    [[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath withIntermediateDirectories:NO attributes:nil error:NULL];
}
else
{
    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath: imageCacheDirPath error: &error];
    [[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath withIntermediateDirectories:NO attributes:nil error:NULL];
 }

while (currentWebViewHeight > 0)
{
    imageName ++;
     UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);
    [self.printWebView.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString *pngPath = [imageCacheDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
    if(currentWebViewHeight < 440)
    {
        CGRect lastImageRect = CGRectMake(0,440-currentWebViewHeight, self.printWebView.frame.size.width, currentWebViewHeight);
        CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
        newImage = [UIImage imageWithCGImage:lastImageRef];
        CGImageRelease(lastImageRef);
    }
    [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
    [self.printWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,440);"];
    currentWebViewHeight -= 440;
}
[self drawPdf];
}

- (void) drawPdf
{
CGSize pageSize = CGSizeMake(612, webViewHeight);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [documentsDirectory stringByAppendingPathComponent:@"pdfImages"];

NSString *pdfFileName = [imageCacheDirPath stringByAppendingPathComponent:fileName];
NSLog(@"file path:%@",pdfFileName);

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

double currentHeight = 0.0;
for (int index = 1; index <= imageName ; index++)
{
    NSString *pngPath = [imageCacheDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]];
    UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];

    [pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
    currentHeight += pngImage.size.height;
}
UIGraphicsEndPDFContext();
}

【讨论】:

  • 这很好,除非您真的希望 PDF 不是一大堆图像。在这种情况下,它是没有用的。要制作可搜索的 PDF(例如,将文本视为文本并将图像视为图像的 PDF),您不能只将其呈现为图像。
  • 好的,看看stackoverflow.com/questions/5817840/… 可能会有所帮助
  • 可搜索的 PDF 可能是我要走的路,所以代码不会为我完成这项工作.. 但为你的努力投了 1 票。
猜你喜欢
  • 2023-04-04
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
相关资源
最近更新 更多