Print PDF with PDFKit.(Objective C)

545 Views Asked by At

I am developing an app that requires previewing and printing of the document. I am able to preview it but not able to print it.

PS: I searched on the web and found that there is a method printwithinfo, but when i am trying to use it it is saying "Unrecognised Method"

Here is the code I am using.

PDFView *pdfView = [[PDFView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:pdfView];
    pdfView.translatesAutoresizingMaskIntoConstraints = NO;
    UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
    [pdfView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
    [pdfView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
    [pdfView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
    [pdfView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
    
    pdfView.autoScales = YES;
    pdfView.displayDirection = kPDFDisplayDirectionVertical;
    pdfView.displaysRTL = YES;
    pdfView.displayMode = kPDFDisplaySinglePageContinuous;
    [pdfView setDisplaysPageBreaks:YES];
    [pdfView setDisplayBox:kPDFDisplayBoxTrimBox];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
    PDFDocument * document = [[PDFDocument alloc] initWithURL:url];
    pdfView.document = document;
    [pdfView.document allowsPrinting];
    
    PDFThumbnailView *thumbnailView = [[PDFThumbnailView alloc]init];
    thumbnailView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:thumbnailView];
    
    [thumbnailView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
    [thumbnailView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
    [thumbnailView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor constant:10].active = YES;
    [thumbnailView.heightAnchor constraintEqualToConstant:120].active = YES;
    thumbnailView.thumbnailSize = CGSizeMake(100, 60);
    thumbnailView.layoutMode = PDFThumbnailLayoutModeHorizontal;
    thumbnailView.PDFView = pdfView;
    
    pdfView.delegate = self;
    pdfView.document.delegate = self;
    pdfView.displaysAsBook = YES;   
1

There are 1 best solutions below

0
Dirk On

After setting up your pdfView, you just need to call

[pdfView printWithInfo:[NSPrintInfo sharedPrintInfo] autorotate:YES]

You probably also have a self.printInfo you can use, but I don't know what self is in your case.