sgr-ksmt / PDFGenerator

A simple generator of PDF written in Swift.
MIT License
757 stars 110 forks source link

Cannot open the generated pdf file. #59

Open Sweeper777 opened 7 years ago

Sweeper777 commented 7 years ago

I am writing an app where users can dynamically generates a pdf file and sends it to people using Mail. I used this code for testing:

    if( MFMailComposeViewController.canSendMail() ) {

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self
    mailComposer.modalPresentationStyle = .formSheet

        // self.view.viewWithTag(1) returns a UITextView that has the Lorem Ipsum text in it
        if let pdf = try? PDFGenerator.generated(by: [self.view.viewWithTag(1)!]) {
            mailComposer.addAttachmentData(pdf, mimeType: "application/pdf", fileName: "lorem")
            self.presentVC(mailComposer)
        }
    }

When the above code is run on my phone, I can see an email compose VC pops up. In the email, there is the pdf attachment. After I send it and tried to open the pdf file on my mac however, Preview.app says that it can't open it.

I think I used this pod wrongly. What did I do wrong?