swiftswiftuipdfdocument

I need to build pdf viewer but with 2 urls to pdfs file


I try to combine two pdf urls file to one PDFView.

import Foundation
import PDFKit


protocol PdfViewDelegate: class {
func heightDidReccived(height:CGFloat)
}

class PdfView: YITStoryboardCustomXibView {
weak var delegate: PdfViewDelegate?

@IBOutlet weak var pageNumbersLabel: UILabel!
@IBOutlet weak var showThumbnailButton: UIButton!
@IBOutlet weak var pdfViewContainer: UIView!
@IBOutlet weak var thumbnailViewContainer: UIView!

@IBOutlet weak var pageNumbersView: UIView!
@IBOutlet weak var pdfViewContainerHeight: NSLayoutConstraint!
@IBOutlet weak var thumbnailViewHeightConstraint: NSLayoutConstraint!

var pdfView = PDFView()
var subData = Data()
let pageCount = 1
var pageReturnCount = 0

override func awakeFromNib() {
    super.awakeFromNib()
}

override func layoutSubviews() {
    pdfScaleFactor()
}

override func xibName() -> String {
    return "PdfView"
}

func openPdf(url: String) {
    let webService = WebService()
    webService.delegate = self
    webService.getRequestFor(url: "http://www.orimi.com/pdf-test.pdf")
    
    let webService1 = WebService()
    webService1.delegate = self
    webService1.getRequestFor(url: "http://www.pdf995.com/samples/pdf.pdf")
}


func getUrl(name:String) -> URL{
    let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
    return resourceDocPath.appendingPathComponent("\(name).pdf")
}

func  configurPdfView(data:Data){
    self.pdfView = PDFView()
    if  let document = PDFDocument(data: subData) {
        pdfView.autoresizesSubviews = true
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
        pdfView.displayDirection = .horizontal
        pdfView.autoScales = true
        pdfView.displaysRTL = true
        pdfView.displayMode = .singlePageContinuous
        pdfView.document = document
        pdfView.backgroundColor = .clear
        pdfViewContainer.addViewWithConstraint(view: pdfView, toView: pdfViewContainer)
        pdfView.usePageViewController(true, withViewOptions:["interPageSpacing": -50])
        
        pdfViewContainer.layer.masksToBounds = false
        pdfViewContainer.layer.shadowOffset = CGSize(width: 0, height: 7)
        pdfViewContainer.layer.shadowRadius = 10
        pdfViewContainer.layer.shadowOpacity = 0.16
    }
}

@IBAction func showThumbnailView(_ sender: Any) {
    
}

private func pdfScaleFactor(){
    pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}

@objc func updatePageChange() {
    if let pageNumber =  pdfView.currentPage?.pageRef?.pageNumber,let total =  pdfView.document?.pageCount {
        pageNumbersLabel.text = "\(pageNumber) מתוך \(total)"
    }
}
 }

extension PdfView : WebServiceDelegate {
func didReceiveData(data: Data) {
    print("subData 1\(data.count)\n")
    print("subData 2\(subData.count)\n")
    subData.append(data)

    if pageCount != pageReturnCount {
        pageReturnCount = pageReturnCount + 1
    }else{
        DispatchQueue.main.async {
            self.configurPdfView(data: self.subData)
        }
    }
}
}

class WebService

class WebService: NSObject {
weak var delegate: WebServiceDelegate?
func getRequestFor(url:String) {
    print("getRequestFor : \(url) \n")
    if WebService.isConectedToInternet(){
        AF.request(url,method:.get)
            .response { response in
                if let data = response.data {
                    DispatchQueue.global(qos: .background).async {
                      self.delegate?.didReceiveData(data: data)
                    }
                }
        }
    }
}

first I try to :

but always the PDFView load the last pdf file and not combine file1 and file2, I saw that data is bigger after the combine data1 and data2.

second :

I try to combine two PDFDocument but still not working.

what I do wrong? it possible to do something like that ?


Solution

  • After you take the data1 and data2 convert it PDF instances and you can use this function :

    func combinePdf(data1: Data, data2: Data) -> PDFDocument {
            let pdfDocument = PDFDocument(data: data1)!
            let otherPdfDocument = PDFDocument(data: data2)!
            
            let newPdfDocument = PDFDocument()
        
            for p in 0..<pdfDocument.pageCount {
            let page = pdfDocument.page(at: p)!
                newPdfDocument.insert(page, at: newPdfDocument.pageCount)
            }
        
            for q in 0..<otherPdfDocument.pageCount {
                let page = otherPdfDocument.page(at: q)!
                newPdfDocument.insert(page, at: newPdfDocument.pageCount)
            }
            return newPdfDocument
        }