Why is HTML to PDF conversion fail on iPad but work on Simulator

My app displays a HTML page using WKWebView and then converts the page to PDF using a function that I copied from SO. The conversion works on the simulator but fails on an actual device. As shown below, the size of the pdf data is correct on the simulator but considerably shorter on the device indicating a failure to convert. I can view the converted PDF on the simulator. The viewController is shown below. I am using OS 10.3. I also tested on iPhone 6+ and the simulator. Same problem.

import UIKit
import WebKit

class ViewController: UIViewController {

    var webView: WKWebView?

    var dict = [String:String]()

    func fileToURL(file: String) -> URL? {
        // Get the full path of the file
        guard let document = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first) else {
            return nil
        }
        return URL(fileURLWithPath: document+file)
    }


    var activityButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()



        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "PDF", style: .plain, target: self, action: #selector(exportHTMLContentToPDF))
        let leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(backAction))
        navigationItem.leftBarButtonItem = leftBarButtonItem

        let config = WKWebViewConfiguration()
        let frame = view.frame
        webView = WKWebView(frame:frame , configuration:config)
        webView?.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        let reportFilename = "report"
        createWebView(htmlFileName: reportFilename)




    }



    override func viewDidAppear(_ animated: Bool) {

    }


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


    func createWebView(htmlFileName: String) {
        let url = Bundle.main.url(forResource: htmlFileName, withExtension: "html")!

        let webView = WKWebView()
        webView.loadFileURL(url, allowingReadAccessTo: url)
        webView.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(webView)

        // Auto Layout
        let views = ["webView": webView]
        let c1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView]|", options: [], metrics: nil, views: views)
        let c2 = NSLayoutConstraint.constraints(withVisualFormat: "V:[webView]|", options: [], metrics: nil, views: views)
        let c3 = NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide , attribute: .bottom, multiplier: 1, constant: 0)
        NSLayoutConstraint.activate(c1 + c2 + [c3])

        // Pass the reference to the View's Controller
        self.webView = webView
        URLCache.shared.removeAllCachedResponses()
        URLCache.shared.diskCapacity = 0
        URLCache.shared.memoryCapacity = 0


        let request = URLRequest(url: url)
        self.webView?.load(request)

    }



    func backAction() {

        dismiss(animated: true, completion: nil)

    }




    func exportHTMLContentToPDF(sender: UIBarButtonItem) {
        let filename = "report.pdf"
        createPdfFromView(aView: webView!, saveToDocumentsWithFileName: filename)

        //pdfDataWithWebView(webView: webView!, view:self.view, filename: filename)

    }

    func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String)
    {

        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil)
        UIGraphicsBeginPDFPage()

        guard let pdfContext = UIGraphicsGetCurrentContext() else {
            print("ERROR: ",(#function))
            return }

        aView.layer.render(in: pdfContext)
        UIGraphicsEndPDFContext()

        if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
            let documentsFileName = documentDirectories + "/" + fileName
            debugPrint(documentsFileName)
            pdfData.write(toFile: documentsFileName, atomically: true)
            let filePath = documentsFileName
            var fileSize : UInt64

            do {
                //return [FileAttributeKey : Any]
                let attr = try FileManager.default.attributesOfItem(atPath: filePath)
                fileSize = attr[FileAttributeKey.size] as! UInt64

                //if you convert to NSDictionary, you can get file size old way as well.
                let dict = attr as NSDictionary
                fileSize = dict.fileSize()
                print("fileSize = \(fileSize)")
            } catch {
                print("Error: \(error)")
            }

            print("SAVED: \(documentsFileName)")
        }

    }



}

The full project is testHTMLtoPDF The console log for both cases is shown below:

Running on Simulator (iPad Air 2)

fileSize = 302726

Running on Device: iPad4 (128 G) 2017-06-17 13:56:34.865220-0400 testHTMLtoPDF[20652:5079739] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service 2017-06-17 13:56:34.871854-0400 testHTMLtoPDF[20652:5079739] Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service "/var/mobile/Containers/Data/Application/E280AA2C-CF4D-4B9C-8777-CD4B87C95320/Documents/report.pdf"

fileSize = 3812