2

I'm trying to create and save pdf for a report in my apps. I search and found this -- Create and store PDF document programmatically using Swift for iOS really helpful, it create pdf from html which is great but it doesn't work for <img>. I want the logo to be appear in my report/pdf.

It worked and image showed when I try it on browser, but the image is not saved into the pdf after I check back the file. I've search and try to do an base64 encoded image but failed too.

Or is there any better way to design and create beautiful pdf for reporting?

Thanks in advance.

Community
  • 1
  • 1
Lee
  • 850
  • 11
  • 23

2 Answers2

2

I have been at this for some time, and finally some breakthrough

Since you are loading the html string, it won't work for images, you have to load the html in a web view and then give UIViewPrintFormatter format as _webVeiw.viewPrintFormatter()

Also don't forget to set baseURL for UIWebView else it won't load the local images.

please look at my gist for more help.

Generate PDF from html iOS swift

I also had troubles when creating page breaks, so I created multiple html's to accommodate that too.

Hope it helps, please let me know if you need any more information :)

vinbhai4u
  • 1,329
  • 3
  • 19
  • 36
  • Sorry for late reply, i accidentally left my mac at office. May I know what is _webView u said? Is it the outlet of webview? – Lee Feb 22 '16 at 01:31
  • I tried your suggestion but load in webview affect the font and size. And for your custom class, i can't see anything except your footer – Lee Feb 22 '16 at 02:42
  • _webview is a simple UIWebView, yes its an outlet, Please check the https://gist.github.com/vinbhai4u/899869df4f1538ffc7d6#file-pdf-creation-from-ios-swift too – vinbhai4u Feb 22 '16 at 04:24
  • Yes i had read it, but not really understand. However, i did follow what u said to load html using webView and it really showed the image though it is not arranged properly. But i can save the image in webview to pdf so I'm going to have a try on that. – Lee Feb 22 '16 at 09:03
  • I have only copied the necessary code, rest is app based which I cannot show in public, Please let me know if u face any more problems, Good luck, It took me a while to figure out all of the above. So happy to help :) – vinbhai4u Feb 22 '16 at 11:40
1

I had same problem, try this code:

    let render = UIPrintPageRenderer()
    render.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0)

    let paperRect = CGRect(x: 0, y: 0, width: 595.2, height: 841.8)
    let printableRect = paperRect.insetBy(dx: 10, dy: 10)

    render.setValue(NSValue(cgRect: paperRect), forKey: "paperRect")
    render.setValue(NSValue(cgRect: printableRect), forKey: "printableRect")

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil)

    for i in 0 ..< render.numberOfPages {
        UIGraphicsBeginPDFPage()
        let bounds = UIGraphicsGetPDFContextBounds()
        render.drawPage(at: i, in: bounds)
    }
    UIGraphicsEndPDFContext()
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let filePath = "\(documentsPath)/name.pdf"
    pdfData.write(toFile: filePath, atomically: true)

    let url = URL(fileURLWithPath: filePath)
    webView.loadRequest(urlRequest)
ArturC
  • 556
  • 6
  • 10