gpt4 book ai didi

ios - 保存后自动打开pdf文件

转载 作者:行者123 更新时间:2023-11-30 12:51:22 25 4
gpt4 key购买 nike

当我按下回车按钮时,我的应用程序会创建一个 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
}

最佳答案

您可以使用 UIDocumentInteractionController 打开 PDF 文件并找到以下代码:

func showFileWithPath(path: String){
let isFileFound:Bool? = NSFileManager.defaultManager().fileExistsAtPath(path)
if isFileFound == true{
let viewer = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: path))
viewer.delegate = self
viewer.presentPreviewAnimated(true)
}
}

关于ios - 保存后自动打开pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40905117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com