我需要构建 pdf 查看器,但有 2 个 url 到 pdfs 文件

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

我尝试将两个 pdf url 文件合并到一个 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 网络服务

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)
                    }
                }
        }
    }
}

首先我尝试:

但 PDFView 总是加载最后一个 pdf 文件而不是合并 file1 和 file2,我看到合并 data1 和 data2 后数据更大。

秒:

我尝试合并两个 PDFDocument,但仍然无法正常工作。

我做错了什么?可以做类似的事情吗?

在你把data1data2转换成PDF实例后,你就可以使用这个功能了:

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
    }