Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to support Vapor 3 #5

Merged
merged 8 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
kimdv marked this conversation as resolved.
Show resolved Hide resolved
"object": {
"pins": [
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
"state": {
"branch": null,
"revision": "74cfbea629d4aac34a97cead2447a6870af1950b",
"version": "3.1.1"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "2731f8ba0cf274a61c9bd6ab43550f692ffaf879",
"version": "3.9.0"
}
},
{
"package": "Crypto",
"repositoryURL": "https://github.com/vapor/crypto.git",
"state": {
"branch": null,
"revision": "df8eb7d8ae51787b3a0628aa3975e67666da936c",
"version": "3.3.3"
}
},
{
"package": "DatabaseKit",
"repositoryURL": "https://github.com/vapor/database-kit.git",
"state": {
"branch": null,
"revision": "8f352c8e66dab301ab9bfef912a01ce1361ba1e4",
"version": "1.3.3"
}
},
{
"package": "HTTP",
"repositoryURL": "https://github.com/vapor/http.git",
"state": {
"branch": null,
"revision": "254a0a0cbf22a02b697a075a0d2ddbb448bb7c87",
"version": "3.2.0"
}
},
{
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"state": {
"branch": null,
"revision": "f919a01c4d10a281d6236a21b0b1d1759a72b8eb",
"version": "3.0.4"
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"state": {
"branch": null,
"revision": "626190ddd2bd9f967743b60ba6adaf90bbd2651c",
"version": "3.0.2"
}
},
{
"package": "Service",
"repositoryURL": "https://github.com/vapor/service.git",
"state": {
"branch": null,
"revision": "fa5b5de62bd68bcde9a69933f31319e46c7275fb",
"version": "1.0.2"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd",
"version": "1.14.1"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "0f3999f3e3c359cc74480c292644c3419e44a12f",
"version": "1.4.0"
}
},
{
"package": "swift-nio-ssl-support",
"repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git",
"state": {
"branch": null,
"revision": "c02eec4e0e6d351cd092938cf44195a8e669f555",
"version": "1.0.0"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
}
},
{
"package": "TemplateKit",
"repositoryURL": "https://github.com/vapor/template-kit.git",
"state": {
"branch": null,
"revision": "121ae51433df94cf6e15c09e1f1b0f7c77ff8d5c",
"version": "1.2.0"
}
},
{
"package": "URLEncodedForm",
"repositoryURL": "https://github.com/vapor/url-encoded-form.git",
"state": {
"branch": null,
"revision": "82d8d63bdb76b6dd8febe916c639ab8608dbbaed",
"version": "1.0.6"
}
},
{
"package": "Validation",
"repositoryURL": "https://github.com/vapor/validation.git",
"state": {
"branch": null,
"revision": "4de213cf319b694e4ce19e5339592601d4dd3ff6",
"version": "2.1.1"
}
},
{
"package": "Vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "c86ada59b31c69f08a6abd4f776537cba48d5df6",
"version": "3.3.0"
}
},
{
"package": "WebSocket",
"repositoryURL": "https://github.com/vapor/websocket.git",
"state": {
"branch": null,
"revision": "d85e5b6dce4d04065865f77385fc3324f98178f6",
"version": "1.1.2"
}
}
]
},
"version": 1
}
21 changes: 20 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
// swift-tools-version:4.2
import PackageDescription

let package = Package(
name: "wkhtmltopdf",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
kimdv marked this conversation as resolved.
Show resolved Hide resolved
.library(
name: "wkhtmltopdf",
targets: ["wkhtmltopdf"]),
],
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "wkhtmltopdf",
dependencies: [
"Vapor"
]),
.testTarget(
name: "wkhtmltopdfTests",
dependencies: ["wkhtmltopdf"]),
]
)
80 changes: 42 additions & 38 deletions Sources/wkhtmltopdf/Document+Generate.swift
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
import Foundation
import Core
import Vapor

#if os(Linux) && !swift(>=3.1)
kimdv marked this conversation as resolved.
Show resolved Hide resolved
typealias Process = Task
#endif

extension Document {

public func generatePDF() throws -> Bytes {
// Create the temp folder if it doesn't already exist
let workDir = "/tmp/vapor-wkhtmltopdf"
try FileManager().createDirectory(atPath: workDir, withIntermediateDirectories: true)
// Save input pages to temp files, and build up args to wkhtmltopdf
var wkArgs: [String] = [
"--zoom", Document.zoom,
"--quiet",
"-s", paperSize,
"-T", "\(topMargin)mm",
"-R", "\(rightMargin)mm",
"-B", "\(bottomMargin)mm",
"-L", "\(leftMargin)mm",
]
let fm = DataFile(workDir: workDir)
let pageFiles: [String] = try pages.map { page in
let fileName = "\(workDir)/\(UUID().uuidString).html"
try fm.write(page.content, to: fileName)
return fileName
}
defer {
pageFiles.forEach { path in
try? fm.delete(at: path)
}
}
wkArgs += pageFiles
// Call wkhtmltopdf and retrieve the result data
let wk = Process()
let stdout = Pipe()
wk.launchPath = "/usr/local/bin/wkhtmltopdf"
wk.arguments = wkArgs
wk.arguments?.append("-") // output to stdout
wk.standardOutput = stdout
wk.launch()
let pdf = stdout.fileHandleForReading.readDataToEndOfFile()
return pdf.makeBytes()
}
public func generatePDF(on req: Request) throws -> Future<Data> {
kimdv marked this conversation as resolved.
Show resolved Hide resolved
let sharedThreadPool = try req.make(BlockingIOThreadPool.self)

return sharedThreadPool.runIfActive(eventLoop: req.eventLoop) { () -> Data in
let fileManager = FileManager.default
// Create the temp folder if it doesn't already exist
let workDir = "/tmp/vapor-wkhtmltopdf"
try fileManager.createDirectory(atPath: workDir, withIntermediateDirectories: true)
// Save input pages to temp files, and build up args to wkhtmltopdf
var wkArgs: [String] = [
"--zoom", Document.zoom,
"--quiet",
"-s", self.paperSize,
"-T", "\(self.topMargin)mm",
"-R", "\(self.rightMargin)mm",
"-B", "\(self.bottomMargin)mm",
"-L", "\(self.leftMargin)mm",
]

let pageFiles: [String] = try self.pages.map { page in
let name = UUID().uuidString + ".html"
let filename = "\(workDir)/\(name)"
try File(data: page.content, filename: filename).data.write(to: URL(fileURLWithPath: filename))
kimdv marked this conversation as resolved.
Show resolved Hide resolved
return filename
}
defer {
try? pageFiles.forEach(fileManager.removeItem)
}

wkArgs += pageFiles
// Call wkhtmltopdf and retrieve the result data
let wk = Process()
let stdout = Pipe()
wk.launchPath = "/usr/local/bin/wkhtmltopdf"
wk.arguments = wkArgs
wk.arguments?.append("-") // output to stdout
wk.standardOutput = stdout
wk.launch()
let pdf = stdout.fileHandleForReading.readDataToEndOfFile()
return pdf
}
}
}
32 changes: 16 additions & 16 deletions Sources/wkhtmltopdf/Document.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
public class Document {

// This may need changing across different platforms and deployments.
static var zoom: String = "1.3"
// This may need changing across different platforms and deployments.
static var zoom: String = "1.3"
kimdv marked this conversation as resolved.
Show resolved Hide resolved

let topMargin: Int
let rightMargin: Int
let bottomMargin: Int
let leftMargin: Int
let topMargin: Int
let rightMargin: Int
let bottomMargin: Int
let leftMargin: Int

let paperSize: String
let paperSize: String

public var pages: [Page] = []

public init(size: String = "A4", margins all: Int? = nil, top: Int? = nil, right: Int? = nil, bottom: Int? = nil, left: Int? = nil) {
paperSize = size
topMargin = all ?? top ?? 20
rightMargin = all ?? right ?? 20
bottomMargin = all ?? bottom ?? 20
leftMargin = all ?? left ?? 20
}
public var pages: [Page] = []
public init(size: String = "A4", margins all: Int? = nil, top: Int? = nil, right: Int? = nil, bottom: Int? = nil, left: Int? = nil) {
paperSize = size
topMargin = all ?? top ?? 20
rightMargin = all ?? right ?? 20
bottomMargin = all ?? bottom ?? 20
leftMargin = all ?? left ?? 20
}

}
13 changes: 0 additions & 13 deletions Sources/wkhtmltopdf/Page+View.swift

This file was deleted.

18 changes: 8 additions & 10 deletions Sources/wkhtmltopdf/Page.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Bits
import Foundation

public struct Page {
let content: Data

let content: Bytes

public init(_ content: Bytes) {
self.content = content
}

public init(_ content: String) {
self.content = content.makeBytes()
}
public init(_ content: Data) {
self.content = content
}

public init(_ content: String) {
self.content = Data(content.utf8)
}
}
Loading