Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从Html创建pdf_Iphone_Objective C - Fatal编程技术网

Iphone 从Html创建pdf

Iphone 从Html创建pdf,iphone,objective-c,Iphone,Objective C,如何从html创建pdf,而不在iphone应用程序的objective c中拍摄uiwebview的屏幕截图或图像 当我创建pdf以捕获UIwebView的屏幕截图时,pdf分辨率看起来不太好。在放大或缩小其分发文本的像素时。 这是一个很好的问题,具有相同的方法 但是,要以编程方式创建pdf,可以从这里开始 在iPhone上生成PDF: 以及: 使用Quartz2d绘制: 我希望这将对您有所帮助。您可以使用quartz core在PDF上下文中绘制UIwebview,而不是将其作为屏幕截图

如何从html创建pdf,而不在iphone应用程序的objective c中拍摄uiwebview的屏幕截图或图像

当我创建pdf以捕获UIwebView的屏幕截图时,pdf分辨率看起来不太好。在放大或缩小其分发文本的像素时。

这是一个很好的问题,具有相同的方法

但是,要以编程方式创建pdf,可以从这里开始 在iPhone上生成PDF:

以及:

使用Quartz2d绘制:


我希望这将对您有所帮助。

您可以使用quartz core在PDF上下文中绘制UIwebview,而不是将其作为屏幕截图。是代码。

UIWebView
使用
UIPrintPageRenderer
UIWebView
执行以下步骤:

添加
UIPrintPageRenderer
类别
,以获取PDF数据 现在在
UIWebView的
webViewDidFinishLoad
delegate
中,使用
UIWebView
UIPrintPageRenderer
属性。

谢谢你的回复。我使用过这种方法,但在这种技术中,当我放大或缩小pdf时,它看起来很模糊。如果您有其他技巧,请回复我。@SauravSinha您可能希望检查我的链接以及石英2d链接的绘图,如Vignesh saidThanks Rohan对我的支持。该链接不再有效。感谢回复。但当我放大或缩小pdf时,它看起来很模糊。您必须在放大后再次绘制pdf,以消除错误模糊效果。@SauravSinha。很高兴它有用!。这对我也有用。但是您是如何定义CGSizeMake(595.2841.8)
?我使用的是iPad Pro 9.7和12.9,那么它们的尺寸是多少?有什么想法吗?由于这种尺寸将pdf压缩为2页,即使底部有很多空间,其A4大小也会压缩为像素,那么DPI呢?我的意思是我们需要根据DPI计算像素吗?
@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end

@implementation UIPrintPageRenderer (PDF)
- (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;
}
@end
#define kPaperSizeA4 CGSizeMake(595.2,841.8)
- (void)webViewDidFinishLoad:(UIWebView *)awebView
{
  if (awebView.isLoading)
    return;

  UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
  [render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];
  //increase these values according to your requirement
  float topPadding = 10.0f;
  float bottomPadding = 10.0f;
  float leftPadding = 10.0f;
  float rightPadding = 10.0f;
  CGRect printableRect = CGRectMake(leftPadding,
                                  topPadding,
                                  kPaperSizeA4.width-leftPadding-rightPadding,
                                  kPaperSizeA4.height-topPadding-bottomPadding);
  CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
  [render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
  [render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
  NSData *pdfData = [render printToPDF];
  if (pdfData) {
    [pdfData writeToFile:[NSString stringWithFormat:@"%@/tmp.pdf",NSTemporaryDirectory()] atomically: YES];
  }
  else
  {
    NSLog(@"PDF couldnot be created");
  }
}