美文网首页vapor 实践
Vapor url生成 pdf

Vapor url生成 pdf

作者: 搞好关系 | 来源:发表于2018-11-15 00:30 被阅读5次

    依赖wkhtmltopdf

     func testXXX(){
        var url =  "https://wkhtmltopdf.org/usage/wkhtmltopdf.txt"
            url = "https://mp.weixin.qq.com/s/lDjpPHwIteggP2yV08i7rQ"
            url = "https://github.com/wkhtmltopdf/wkhtmltopdf/blob/master/examples/image_c_api.c"
            url = "https://mp.weixin.qq.com/s/jJ-S0raM0uc5lah3H6hWcw"
            gennerXPDF(from: url) { (stdout:Pipe) in
                
                let data = stdout.fileHandleForReading.readDataToEndOfFile().makeBytes()
                
                let fm = DataFile(workDir: "/Volumes/Documents/GitHub/wkhtmltopdf")
                try! fm.write(data, to: "/Volumes/Documents/GitHub/wkhtmltopdf/\(url.components(separatedBy: "/").last!).pdf")
    XCTAssert(data.count > 0)
            }
        }
        public func gennerXPDF(from url: String, result:(Pipe)->Void)->Void{
            let wk = Process()
            let stdout = Pipe()
            wk.launchPath = "/usr/local/bin/wkhtmltopdf"
            wk.arguments = [   "--dump-outline","toc" ,url ]
            wk.arguments?.append("-") // output to stdout
            wk.standardOutput = stdout
            wk.launch()
            result(stdout)
        }
    

    可以根据此来生生成PDF发送给用户制作一个稍后阅读的需求
    如:热门文章生成PDF发送到kindle 发送到邮箱等

    相关文章

      网友评论

        本文标题:Vapor url生成 pdf

        本文链接:https://www.haomeiwen.com/subject/atanxqtx.html