-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from kimdv/kimdv/Vapor-3
Updated to support Vapor 3
- Loading branch information
Showing
8 changed files
with
155 additions
and
226 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
// swift-tools-version:4.2 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "wkhtmltopdf", | ||
products: [ | ||
.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"), | ||
.package(url: "https://github.com/vapor/service.git", from: "1.0.0") | ||
], | ||
targets: [ | ||
.target( | ||
name: "wkhtmltopdf", | ||
dependencies: [ | ||
"Service" | ||
]), | ||
.testTarget( | ||
name: "wkhtmltopdfTests", | ||
dependencies: ["wkhtmltopdf", "Vapor"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
import Foundation | ||
import Core | ||
|
||
#if os(Linux) && !swift(>=3.1) | ||
typealias Process = Task | ||
#endif | ||
import Service | ||
|
||
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 container: Container) throws -> Future<Data> { | ||
let sharedThreadPool = try container.make(BlockingIOThreadPool.self) | ||
|
||
return sharedThreadPool.runIfActive(eventLoop: container.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", self.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 page.content.write(to: URL(fileURLWithPath: filename)) | ||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
public class Document { | ||
|
||
// This may need changing across different platforms and deployments. | ||
static var zoom: String = "1.3" | ||
let zoom: String | ||
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", zoom: String? = nil, margins all: Int? = nil, top: Int? = nil, right: Int? = nil, bottom: Int? = nil, left: Int? = nil) { | ||
self.zoom = zoom ?? "1.3" | ||
paperSize = size | ||
topMargin = all ?? top ?? 20 | ||
rightMargin = all ?? right ?? 20 | ||
bottomMargin = all ?? bottom ?? 20 | ||
leftMargin = all ?? left ?? 20 | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.