在iphone应用程序中从UIScrollView创建PDF

在iphone应用程序中从UIScrollView创建PDF,iphone,ios,ipad,uiscrollview,Iphone,Ios,Ipad,Uiscrollview,我正在创建从UIView到pdf的pdf。它工作正常,但我有scrollView内容,我想将其转换为pdf,但它只显示pdf中可见的部分,而不是整个scrollView内容。这是我的密码 -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { NSMutableData *pdfData = [NSMutableData data]; //

我正在创建从UIView到pdf的pdf。它工作正常,但我有
scrollView
内容,我想将其转换为pdf,但它只显示pdf中可见的部分,而不是整个
scrollView
内容。这是我的密码

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
 {    
    NSMutableData *pdfData = [NSMutableData data];

    // Get Scrollview size
    CGRect scrollSize = CGRectMake(1018,76,scrollView.contentSize.width,scrollView.contentSize.height);

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, scrollSize, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];





    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

这将提供UIScrollView的可见部分

CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.bounds.size;

float theScale = 1.0 / scale;
visibleRect.origin.x *= theScale;
visibleRect.origin.y *= theScale;
visibleRect.size.width *= theScale;
visibleRect.size.height *= theScale;
因此,您可以在
UIGraphicsBeginPDFContextToData(pdfData,aView.bounds,nil)中使用它

通过对参数进行一些细微的更改。

这段代码我用来从uiscrollview生成pdf,它确实有帮助,但它不会像我们从pdf中提取一样好--请查看--来自uiscrollview的pdf

(void)createPDFfromUIView:(UIScrollView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Get Scrollview size
    CGRect scrollSize = CGRectMake(aView.origin.x,aView.origin.y,aView.contentSize.width,aView.contentSize.height);

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, scrollSize, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
- (void) createPDF
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *directroyPath = nil;
    directroyPath = [documentsDirectory stringByAppendingPathComponent:@"temp"];
    NSString *filePath = [directroyPath stringByAppendingPathComponent:@"test.pdf"];
    // check for the "PDF" directory
    NSError *error;
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

    } else {
        [[NSFileManager defaultManager] createDirectoryAtPath:directroyPath
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:&error];
    }

    CGContextRef pdfContext = [self createPDFContext:_scrollView2.bounds path:(CFStringRef)filePath];
    NSLog(@"PDF Context created");

    /*
     Here limit of i is no of pages in your uiscrollview or
     you can use hit and trial here because there is a
     length of page is going to be equal to 
     the visible size of uiscrollView in your application 
   */

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

        // page 1
        CGContextBeginPage (pdfContext,nil);

        //turn PDF upsidedown
        CGAffineTransform transform = CGAffineTransformIdentity;

       //here 365 is equal to the height of myScrollView.frame.size.height

        transform = CGAffineTransformMakeTranslation(0, (i+1) * 365);
        transform = CGAffineTransformScale(transform, 1.0, -1.0);
        CGContextConcatCTM(pdfContext, transform);

        //Draw view into PDF
        [_scrollView2.layer renderInContext:pdfContext];
        CGContextEndPage (pdfContext);
        [_scrollView2 setContentOffset:CGPointMake(0, (i+1) * 365) animated:NO];

    }
    CGContextRelease (pdfContext);
}

- (CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{
    CGContextRef myOutContext = NULL;
    CFURLRef url;
    url = CFURLCreateWithFileSystemPath (NULL, path,
                                         kCFURLPOSIXPathStyle,
                                         false);

    if (url != NULL) {
        myOutContext = CGPDFContextCreateWithURL (url,
                                                  &inMediaBox,
                                                  NULL);
        CFRelease(url);
    }
    return myOutContext;
}
-(无效)创建PDF
{
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*directroyPath=nil;
directroyPath=[DocumentsDirectoryStringByAppendingPathComponent:@“temp”];
NSString*filePath=[directroyPath stringByAppendingPathComponent:@“test.pdf”];
//检查“PDF”目录
n错误*错误;
if([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
}否则{
[[NSFileManager defaultManager]创建目录路径:directroyPath
中间目录:否
属性:无
错误:&错误];
}
CGContextRef pdfContext=[self-createPDFContext:_-scrollView2.bounds路径:(CFStringRef)文件路径];
NSLog(@“创建了PDF上下文”);
/*
这里的i限制是您的uiscrollview或
您可以在这里使用点击和试用,因为有一个
页面的长度将等于
应用程序中uiscrollView的可见大小
*/
对于(int i=0;i<2;i++)
{
//第1页
CGContextBeginPage(pdfContext,无);
//翻转PDF
CGAffineTransform变换=CGAffineTransformity;
//此处365等于myScrollView.frame.size.height的高度
transform=CGAffineTransformMakeTransform(0,(i+1)*365);
变换=CGAffineTransformScale(变换,1.0,-1.0);
CGContextConcatCTM(pdfContext,transform);
//将视图绘制为PDF
[\u scrollView2.layer renderInContext:pdfContext];
CGContextEndPage(pdfContext);
[_ScrollView2SetContentOffset:CGPointMake(0,(i+1)*365)动画:否];
}
CGContextRelease(pdfContext);
}
-(CGContextRef)createPDFContext:(CGRect)inMediaBox路径:(CFStringRef)路径
{
CGContextRef myOutContext=NULL;
cfurlRefURL;
url=cfurlCreateWithFileSystems(NULL,路径,
kCFURLPOSIXPathStyle,
假);
如果(url!=NULL){
myOutContext=CGPDFContextCreateWithURL(url,
&inMediaBox,
无效);
CFRelease(url);
}
返回myOutContext;
}

将scrollview内容转换为PDF文件的最简单方法是将内容视图作为scrollview的第一个子视图,并使用该视图拍摄scrollview的快照。Swift中的代码如下所示:

func generatePDFdata(withView view: UIView) -> NSData {

  let pageDimensions = view.bounds
  let outputData = NSMutableData()

  UIGraphicsBeginPDFContextToData(outputData, pageDimensions, nil)
  if let context = UIGraphicsGetCurrentContext() {
      UIGraphicsBeginPDFPage()
      view.layer.render(in: context)
  }
  UIGraphicsEndPDFContext()

  return outputData
}
然后,您可以使用
outputData
写入PDF文件:

outputData.write(to: pdfFileUrl, atomically: true)

在何处编写此代码请您在我的代码中编辑请什么是比例请您帮助我
scale
假设是UIScrollView的宽度相同的问题它只是显示可见区域而不是整个视图您需要ScrolVIew ryt的全部数据..?访问此答案,它将帮助您@Yuvrajsinh这是我正在使用的同一代码,但它是用于普通而不是UIScrollViews的。我认为问题是,您正在创建具有静态大小的新UIView。使用yourScrollView.contentSize获取确切的内容大小(宽度、高度)在你的scrollview中,不需要创建新的视图。你能在我的代码中编辑吗?你最终得到了正确的答案吗?aView.originalx我们必须给出scrollview的原始位置这些答案有一些棘手的解决方案参考它:我已经检查了这两个,但我没有从中弄清楚检查我的更新代码显示滚动条,但它不显示任何内容。这意味着它获取滚动视图大小,但不获取内容