ios – Generating a pdf and attaching to email in swift Xcode 13

0
196


I am hoping someone can help me with an issue I am having. what I am doing is generating a pdf in my app using simplePdf Cocapod. https://cocoapods.org/pods/SimplePDF . This works fine and I can prevent the pdf with all the information I need in the standard Vc and then share it from here, however what I would like to do is open a mail compose view controller and attach the file automatically so I have the mail function working fine and I can fill out all the information I need, I just can’t workout how to attach the pdf. any help would be greatly appreciated, thanks

extension SecondViewController {

func pdfCreate() {
   let pdf = PDFBuilder()

   //pdf.wihMetaAuthor("Maks")
pdf.withMetaCreator("\(Location.text!) \(Date.date)")
   pdf.withMetaTitle(" MISS")
//PDF Sizing etc
pdf.withPaperSize(.A4)              // default value
pdf.withPaperOrientation(.Portrait) // default value
pdf.withTextSpacing(1.08)           // default value (Also Word's default)
pdf.withPaperMargins(.Narrow)       // default value



//location details and date top left
pdf.holdLine()
pdf.addText(text: " \(Date.date)",
               alignment: .left,
               font: .systemFont(ofSize: 15),
               colour: .systemBlue)
// address
pdf.addText(text: "Prem",
            alignment: .right,
            font: .systemFont(ofSize: 15),
            colour: .systemBlue)
//
pdf.releaseLine()

//
pdf.addSpace(centimeters: 0.4)

pdf.addText(text: "\(Description.text!)",
               alignment: .left,
               font: .systemFont(ofSize: 10),
               colour: .black)

pdf.addSpace(centimeters: 0.05)

pdf.addText(text: "\(Action.text!)",
            alignment: .left,
            font: .systemFont(ofSize: 10),
            colour: .black)

pdf.addSpace(centimeters: 0.05)

pdf.addText(text: "\(Prevent.text!)",
            alignment: .left,
            font: .systemFont(ofSize: 10),
            colour: .black)

pdf.addSpace(centimeters: 0.4)
//ADDing image to PDF
pdf.addImage(image: imageView.image!, maxWidth: 190, alignment: .left)

//footer
pdf.addFooter(pagingEnabled: true,
              text: "Pre. \n Development By ",
              colour: UIColor.black.withAlphaComponent(0.5))
   


// Build PDF Data
let data = pdf.build()

let pdfController = PDFPreviewVC(pdfData: data)
navigationController?.pushViewController(pdfController, animated: true)



}

}



 @IBAction func PDFCREATOR(_ sender: Any) {
    print("PDFButton Pushed")
    pdfCreate()
}




func composeMail(with image:UIImage) {
    
    if MFMailComposeViewController.canSendMail(){
        
    

    let mailComposeVC = MFMailComposeViewController()
    
    mailComposeVC.mailComposeDelegate = self
    
    mailComposeVC.setToRecipients(["Nuhnlhoyb"])

    mailComposeVC.addAttachmentData(image.jpegData(compressionQuality:   
CGFloat(1.0))!, mimeType: "image/jpeg", fileName:  "test.jpeg")

    mailComposeVC.setSubject(" \(Location.text!)")

    mailComposeVC.setMessageBody(" \n\n  \(Action.text!) \n\n \(Prevent.text!)",   

enter code hereisHTML: false)

    self.present(mailComposeVC, animated: true, completion: nil)
    } else {
        print("Email not configured in setting app!")
    }
    
}


//end of mail composer with image