簡體   English   中英

如何上傳 PDF Firebase 存儲?

[英]How can I upload a PDF Firebase Storage?

我目前正在嘗試從當前視圖獲取 PDF,然后將其設置為變量,然后我可以將其上傳到 firebase 存儲,並取回鏈接。

我已經使用以下代碼完成了 PDF 的生產:

  func exportToPDF() {

      let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      let outputFileURL = documentDirectory.appendingPathComponent("SwiftUI.pdf")

      //Normal with
      let width: CGFloat = 8.5 * 72.0
      //Estimate the height of your view
      let height: CGFloat = 1000
      let charts = SmartSheet()

      let pdfVC = UIHostingController(rootView: charts)
      pdfVC.view.frame = CGRect(x: 0, y: 0, width: width, height: height)

      //Render the view behind all other views
      let rootVC = UIApplication.shared.windows.first?.rootViewController
      rootVC?.addChild(pdfVC)
      rootVC?.view.insertSubview(pdfVC.view, at: 0)

      //Render the PDF
    let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: 8.5 * 72.0, height: height))
    DispatchQueue.main.async {
        do {
            try pdfRenderer.writePDF(to: outputFileURL, withActions: { (context) in
                context.beginPage()
                pdfVC.view.layer.render(in: context.cgContext)
            })
            print("wrote file to: \(outputFileURL.path)")
        } catch {
            print("Could not create PDF file: \(error.localizedDescription)")
        }
    }

      pdfVC.removeFromParent()
      pdfVC.view.removeFromSuperview()
  }

我只需要知道如何將其上傳到 firebase。

僅供參考 - 我已經能夠使用此代碼上傳 UIImages:

    func upload(image: UIImage) {
        // Create a storage reference
      let storageRef = Storage.storage().reference().child("squawks/\(v.FirstName)-sqauwk.jpg")

        // Resize the image to 200px with a custom extension


        // Convert the image into JPEG and compress the quality to reduce its size
        let data = image.jpegData(compressionQuality: 0.2)

        // Change the content type to jpg. If you don't, it'll be saved as application/octet-stream type
        let metadata = StorageMetadata()
        metadata.contentType = "\(v.FirstName)-sqauwk/jpg"

        // Upload the image
        if let data = data {
            storageRef.putData(data, metadata: metadata) { (metadata, error) in
                if let error = error {
                    print("Error while uploading file: ", error)
                }

                if let metadata = metadata {
                    print("Metadata: ", metadata)
                }
            }
        }
    }


    func getPhotoURL(completion: @escaping (String) -> Void) {
      // Create a reference to the file you want to download
      let starsRef = Storage.storage().reference().child("squawks/\(v.FirstName)-sqauwk.jpg")

      // Fetch the download URL
      starsRef.downloadURL { url, error in
        if let error = error {
          // Handle any errors
          print("error getting link: \(error)")
        } else {
          // Get the download URL for 'images/stars.jpg'
          completion(url!.absoluteString)
        }
      }
    }

//On click of button:
            var strURL = ""
            upload(image: inputImage!) //inputImage is a UIImage
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
              getPhotoURL() { url in
                strURL = url
                print("STORAGE URL: \(strURL)")
              }

謝謝!

upload更改為

func upload(_ pdf: URL) {
   let data  = try! Data(contentsOf:pdf)
   let storageRef = Storage.storage().reference().child("squawks/\(pdf.lastPathComponent)")
   // .... go on

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM