【问题标题】:PDF Generation in Swift Playground在 Swift Playground 中生成 PDF
【发布时间】:2015-11-01 11:43:47
【问题描述】:

是否可以使用 Swift Playground 创建 PDF 文件?
我正在编写一个应用程序,它可以从一组选定的对象中自动生成一个,但是通过该过程检查我所做的每一个更改很快就会变得很痛苦。

我不确定 Playground 在文档目录或其他方面是如何工作的,因为我只将它用于文档。

可以这样做吗?

编辑:

所以我设法通过使用目录NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 从 Playground 生成 PDF

我已将网络视图放入 Playground 以尝试预览它,但没有结果。在操场上还有其他实时方式吗?

Web 视图正在使用正确的 URL 加载请求,因为我在 Finder 中使用相同的 url 导航到它,但它只显示在 Playground 中预览的空白页面,尽管图像出现在 Finder 的 PDF 中

游乐场文件

import UIKit

// Properties
var documentDirectories : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentDirectory : String = documentDirectories[0] as! String
var pdfFileName : String = "bill.pdf"
var pdfPathWithFileName : String = (documentDirectory as NSString).stringByAppendingPathComponent(pdfFileName)
let pageSize : CGSize = CGSize(width: 200, height: 1000)

// Graphics Context
UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil)
UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: 850, height: 1000), nil)
let context : CGContextRef = UIGraphicsGetCurrentContext()!
let rect : CGRect = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)

// Fill background
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, rect)

// Products
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)

for var i = 0; i < 10; i++ {
   let rect : CGRect = CGRect(x: 100, y: (i * 50), width: 300, height: 50)
   let productName : NSString = "ProductNo\(i)"
   productName.drawWithRect(rect, options: .UsesFontLeading, attributes: nil, context: NSStringDrawingContext.init())

   let imageRect : CGRect = CGRect(x: 20, y: (i*50), width: 50, height: 50)
   let image = UIImage(named: "testImage.png")
   image?.drawInRect(imageRect)
}


// End PDF Context
UIGraphicsEndPDFContext()


// PDF Web Request
let pdfURL = NSURL(string: pdfPathWithFileName)
let request = NSURLRequest(URL: pdfURL!)


// Web View
let webSize = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
let webView = UIWebView(frame: webSize)
webView.loadRequest(request)

let scroll : UIScrollView = webView.scrollView

let zoom = webView.bounds.size.width/scroll.contentSize.width
scroll.setZoomScale(zoom, animated: true)

【问题讨论】:

  • 谢谢埃里克。查看更新的问题
  • 不客气。通过显示一些相关代码来帮助我们帮助您。人们或许可以提供帮助,但目前 IMO 的问题有点模糊。
  • 之前是要粘贴到 Playground 文件中的,现在就完成了!

标签: swift pdf-generation swift-playground


【解决方案1】:

如果您激活菜单:

查看 > 调试区 > 显示调试区

然后就可以在控制台看到了:

无法为...获取沙盒扩展

Playground 是沙盒化的,您不能使用它们来操作 Playground 自身资源外部的文件,NSSearchPathForDirectoriesInDomains 在这种情况下毫无用处。

图像(或您要使用的任何文件)必须位于 Playground 的 Resources 文件夹中,您才能通过 UIImage(named:)NSBundle.mainBundle().URLForResource 等方法使用它们。

如果您激活菜单:

查看 > 导航器 > 显示项目导航器

您将能够将图像和文件添加到 Playground 的 Resources 文件夹。

注意:这个 Resources 文件夹实际上是 Playground 自动生成的唯一缓存文件夹中的一个文件夹的链接。您不应该尝试直接使用它的 URL,而是通过 NSBundle(或其他方式)。

【讨论】:

    【解决方案2】:

    我不确定这是否是您想要的,但以下是我将 PDF 文件导入 iPad 版 Swift Playground 的方法:

    首先,创建一个Image 扩展(灵感来自 Paul Hudson 的帖子:How to render a PDF to an image):

    import SwiftUI
    
    // ?Image(pdf: url)
    extension Image {
        
        public init?(pdf url: URL) {
            
            guard 
                let doc  = CGPDFDocument(url as CFURL),
                let page = doc .page(at: 1) 
                else { return nil }
            
            let pageRect = page.getBoxRect(.mediaBox)
            let renderer = UIGraphicsImageRenderer(size: pageRect.size)
            
            let img = renderer.image { ctx in
                UIColor.clear.set()
                ctx.fill(pageRect)
                ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
                ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
                ctx.cgContext.drawPDFPage(page)
            }
            
            self = Image(uiImage: img)
        }
    }
    

    然后将 PDF 文件导入 Playground(在我的 iPad 上运行,如果你想在 Xcode 上工作,你必须自己尝试):

    然后直接使用扩展名,例如:

    import SwiftUI
    import PlaygroundSupport
    
    struct ContentView: View {
        var body: some View {
            VStack {
                Image(pdf: #fileLiteral(resourceName: "Card1.pdf"))
                Image(pdf: #fileLiteral(resourceName: "Card3.pdf"))
            }// container
                .padding()
                .shadow(radius: 8)
                .background(Color.gray)
                .cornerRadius(10)
        }
    }
    
    PlaygroundPage.current.setLiveView(ContentView())
    

    #fileLiteral 隐藏在 Playground 中,它实际上是这样的:

    虽然看起来很奇怪,但它确实有效,结果如下所示:

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多