4

I am creating pdf from UIView to pdf it works fine but i have scrollView with content which i want to convert to pdf but it only show visible part in pdf not the whole scrollView content. here is my code

-(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);
}
Regexident
  • 29,441
  • 10
  • 93
  • 100
Ali Imran
  • 179
  • 4
  • 12
  • Visit this answer, it will help you. http://stackoverflow.com/a/6566696/1223897 – Yuvrajsinh May 17 '13 at 05:37
  • @Yuvrajsinh this is the same code which i am using but it is for plain not for UIScrollView – Ali Imran May 17 '13 at 05:42
  • I think the problem is, you are creating new UIView with static size. Use yourScrollView.contentSize for getting exact content size (width,height) in your scrollview and no need of creating new view. – Yuvrajsinh May 17 '13 at 05:52
  • can you please edit in my code how to do this – Ali Imran May 17 '13 at 06:01
  • Did you get a correct answer finally ? – Seb Mar 25 '14 at 16:12
  • @AliImran Did you get any solution for this ? I am also facing same problem. Getting full height of scrollview but it display only visible portion. – Abha Jul 27 '16 at 06:22

4 Answers4

1

THis gives you the visible portion of the 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;

so you can use this in UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
by some minor changes in the parameters.

Ahmed Z.
  • 2,329
  • 23
  • 52
1
(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);
}
Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
1

This code i used to make pdf from uiscrollview and it does help but it will not be as good as if we draw on pdf -- please have look -- Pdf from UIScrollView

- (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;
}
kshitij godara
  • 1,523
  • 18
  • 30
0

The easiest way to convert the scrollview content into a PDF file is to have a content view as the first subview of the scrollview and using that view to take a snapshot of the scrollview. The code in Swift would look like this:

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
}

You can then use the outputData to write into a PDF file:

outputData.write(to: pdfFileUrl, atomically: true)
Amer Hukic
  • 1,494
  • 1
  • 19
  • 29