我想从html内容生成一个pdf文件。(从html模板生成发票)我想指出的是,我是一个swift-beginner.
要将html内容打印到pdf文件,我使用以下代码:
func makePDF(markup: String) {
let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let printOpts: [NSPrintInfo.AttributeKey: Any] = [NSPrintInfo.AttributeKey.jobDisposition: NSPrintInfo.JobDisposition.save, NSPrintInfo.AttributeKey.jobSavingURL: directoryURL]
let printInfo = NSPrintInfo(dictionary: printOpts)
//printInfo.horizontalPagination = NSPrintInfo.
//printInfo.verticalPagination = NSPrintInfo.AutoPagination
printInfo.topMargin = 20.0
printInfo.leftMargin = 20.0
printInfo.rightMargin = 20.0
printInfo.bottomMargin = 20.0
let view = NSView(frame: NSRect(x: 0, y: 0, width: 570, height: 740))
if let htmlData = markup.data(using: String.Encoding.utf8) {
if let attrStr = NSAttributedString(html: htmlData, options: [ .documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) {
print(attrStr);
let frameRect = NSRect(x: 0, y: 0, width: 570, height: 740)
let textField = NSTextField(frame: frameRect)
textField.attributedStringValue = attrStr
view.addSubview(textField)
let printOperation = NSPrintOperation(view: view, printInfo: printInfo)
printOperation.showsPrintPanel = false
printOperation.showsProgressPanel = false
printOperation.run()
}
}
基于这个StackOverFlow问题/答案:StackOverFlowAnswer
我不得不将代码“翻译”成swift4 --在运行代码时,这里的任何错误都是很有可能的,我得到了以下日志(按照这个顺序):
PMSessionBeginCGDocumentNoDialog() -61根据this (写权限错误),->表示我没有在某个位置写的权限
我尝试切换沙箱模式,并赋予应用程序对桌面文件进行读写的权限。
有人知道如何修复PMSessionBeginCGDocumentNoDialog() -61错误吗?(或者通常让代码启动并运行...)谢谢!
发布于 2018-05-28 18:18:25
https://stackoverflow.com/questions/50563662
复制相似问题