从html转换为pdf时,https图像链接未以pdf格式显示

时间:2017-02-21 07:42:19

标签: ios swift uiwebview swift3

https图片链接未以pdf格式显示,但http图片链接在将PDF转换为pdf时显示在pdf中。但是在UIWebview中,我可以看到两者的图像,任何人都可以帮忙吗?

<img src="https://firebasestorage.googleapis.com/v0/b/getbilleasy1.appspot.com/o/Images%2F1F057F2D-BF61-4EAB-8D0A-D286BEA957C9.png?alt=media&token=95bbd8e7-b12d-4d8f-beda-e911aa3f493f" style="width:100%; max-width:300px; background-color: #cdcdcd">

let logoImageURL = "https://firebasestorage.googleapis.com/v0/b/getbilleasy1.appspot.com/o/Images%2F1F057F2D-BF61-4EAB-8D0A-D286BEA957C9.png?alt=media&token=95bbd8e7-b12d-4d8f-beda-e911aa3f493f"

let logoImageURL = "http://www.alertinfo.in/wp-content/uploads/2016/12/Putin-behind-Trump-win-324x160.jpg" 我正在使用这两个链接

`import UIKit

class InvoiceComposer:NSObject {

let pathToInvoiceHTMLTemplate = Bundle.main.path(forResource: "invoice", ofType: "html")

let pathToSingleItemHTMLTemplate = Bundle.main.path(forResource: "single_item", ofType: "html")

let pathToLastItemHTMLTemplate = Bundle.main.path(forResource: "last_item", ofType: "html")
let senderInfo = UserDefaults.standard.object(forKey: "COMPANYDETAILSPDF") as? String




let dueDate = ""

let paymentMethod = "Cash"

//让logoImageURL = UserDefaults.standard.object(forKey:“LOGO”)为?串     var Image_url:String! 让logoImageURL =“https://firebasestorage.googleapis.com/v0/b/getbilleasy1.appspot.com/o/Images%2F1F057F2D-BF61-4EAB-8D0A-D286BEA957C9.png?alt=media&token=95bbd8e7-b12d-4d8f-beda-e911aa3f493f

//让logoImageURL =“http://www.alertinfo.in/wp-content/uploads/2016/12/Putin-behind-Trump-win-324x160.jpg”     var invoiceNumber:String!

var pdfFilename: String!
  var invoices: [[String: AnyObject]]!

override init() {
    super.init()
}


func renderInvoice(invoiceNumber: String,invoiceDate: String,  recipientInfo: String,  totalAmount: String,items: [[String: String]]) -> String! {

    self.invoiceNumber = invoiceNumber

    do {
        // Load the invoice HTML template code into a String variable.
        var HTMLContent = try String(contentsOfFile: pathToInvoiceHTMLTemplate!)

        // Replace all the placeholders with real values except for the items.
        // The logo image.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: logoImageURL)

        // Invoice number.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#INVOICE_NUMBER#", with: invoiceNumber)

        // Invoice date.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#INVOICE_DATE#", with: invoiceDate)

        // Due date (we leave it blank by default).
        HTMLContent = HTMLContent.replacingOccurrences(of: "#DUE_DATE#", with: dueDate)

        // Sender info.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#SENDER_INFO#", with: senderInfo!)

        // Recipient info.
        print(recipientInfo)
        HTMLContent = HTMLContent.replacingOccurrences(of: "#RECIPIENT_INFO#", with: recipientInfo.replacingOccurrences(of: "\n", with: "<br>"))

        // Payment method.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#PAYMENT_METHOD#", with: paymentMethod)

        // Total amount.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#TOTAL_AMOUNT#", with: totalAmount)

        // The invoice items will be added by using a loop.
        var allItems = ""

        // For all the items except for the last one we'll use the "single_item.html" template.
        // For the last one we'll use the "last_item.html" template.

        print(items.count)
        for i in 0..<items.count {
            var itemHTMLContent: String!

            // Determine the proper template file.
            if i != items.count - 1 {
                itemHTMLContent = try String(contentsOfFile: pathToSingleItemHTMLTemplate!)
            }
            else {
                itemHTMLContent = try String(contentsOfFile: pathToLastItemHTMLTemplate!)
            }

            // Replace the description and price placeholders with the actual values.
            itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#ITEM_DESC#", with: items[i]["item"]!)

            // Format each item's price as a currency value.
            let formattedPrice = items[i]["price"]!
            itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#PRICE#", with: formattedPrice)

            // Add the item's HTML code to the general items string.
            allItems += itemHTMLContent
        }

        // Set the items.
        HTMLContent = HTMLContent.replacingOccurrences(of: "#ITEMS#", with: allItems)



        // The HTML code is ready.
        return HTMLContent

    }
    catch {
        print("Unable to open and use HTML template files.")
    }

    return nil
}


func exportHTMLContentToPDF(HTMLContent: String) {
    let printPageRenderer = CustomPrintPageRenderer()

    let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
    printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)

    let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)

    pdfFilename = "\(AppDelegate.getAppDelegate().getDocDir())/Invoice\(invoiceNumber!).pdf"

    UserDefaults.standard.set(pdfFilename, forKey:"PDFFILENAME")
    UserDefaults.standard.synchronize()
    pdfData?.write(toFile: pdfFilename, atomically: true)

    print(pdfFilename)


}


func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
    let data = NSMutableData()

    UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)

    UIGraphicsBeginPDFPage()

    printPageRenderer.drawPage(at: 0, in: UIGraphicsGetPDFContextBounds())

    UIGraphicsEndPDFContext()

    return data
}

} “对此有什么解决方案吗?

0 个答案:

没有答案