2016-12-01 5 views
0

입력을 누르면 내 응용 프로그램에서 pdf 파일을 만듭니다. Enter 버튼에 하나 이상의 기능을 추가하고 싶습니다. 두 번째 함수는 생성 된 pdf 파일을 메일에 첨부합니다. 그래서 내가 입력하면 pdf 파일이 생성되어 메일에 첨부됩니다.저장 후 pdf 파일을 메일에 자동으로 첨부하십시오.

이 코드는 pdf 파일을 저장할 수 있습니다 :

@IBAction func EnterButtonAction(_ sender: AnyObject) { 

let html = "PDF FILE TITLE" 

let fmt = UIMarkupTextPrintFormatter(markupText: html) 

// 2. Assign print formatter to UIPrintPageRenderer 
let render = UIPrintPageRenderer() 
render.addPrintFormatter(fmt, startingAtPageAt: 0) 

// 3. Assign paperRect and printableRect 
let page = CGRect(x: 0, y: 0, width: 595.2, height: 1000) // A4, 72 dpi 
let printable = page.insetBy(dx: 0, dy: 0) 

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

// 4. Create PDF context and draw 
let pdfData = NSMutableData() 
UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil) 

for i in 1...render.numberOfPages { 

    UIGraphicsBeginPDFPage(); 
    let bounds = UIGraphicsGetPDFContextBounds() 
    render.drawPage(at: i - 1, in: bounds) 
} 

UIGraphicsEndPDFContext(); 

// Save PDF file 
let path = "\(NSTemporaryDirectory())MyAppFile.pdf" 
pdfData.write(toFile: path, atomically: true) 
print("open \(path)") // command to open the generated file 

가} 아래 스위프트 3.0 코드가 큰

+1

http://stackoverflow.com/questions/30423583/attach-a-pdf-file-to-email-swift의 사본 – Darshana

답변

0

일입니다!

let pdfFilePath = "path to file.pdf" 
let url = NSURL.fileURL(withPath: pdfFilePath) 
let activityVC = UIActivityViewController(activityItems: ["My Pdf File",url], applicationActivities: nil) 
self.present(activityVC, animated: true, completion: nil)