All Projects → vapor-community → wkhtmltopdf

vapor-community / wkhtmltopdf

Licence: other
Generate and return PDFs from Vapor views

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to wkhtmltopdf

fluent-provider
A provider for including Fluent in Vapor applications
Stars: ✭ 13 (-75.47%)
Mutual labels:  vapor, vapor-provider
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (+24.53%)
Mutual labels:  vapor, vapor-provider
VaporGCM
A simple Android GCM/FCM library for Swift/Vapor
Stars: ✭ 25 (-52.83%)
Mutual labels:  vapor, vapor-provider
Lingo-Vapor
Vapor provider for Lingo - the Swift localization library
Stars: ✭ 45 (-15.09%)
Mutual labels:  vapor, vapor-provider
mysql-provider
MySQL provider for the Vapor web framework.
Stars: ✭ 31 (-41.51%)
Mutual labels:  vapor, vapor-provider
leaf-markdown
Markdown renderer for Vapor
Stars: ✭ 51 (-3.77%)
Mutual labels:  vapor, vapor-provider
Stripe
Stripe library for Vapor
Stars: ✭ 151 (+184.91%)
Mutual labels:  vapor
Htmlkit
A type-safe DSL that renders dynamic HTML templates in Swift
Stars: ✭ 229 (+332.08%)
Mutual labels:  vapor
Mistkit
Swift Package for Server-Side and Command-Line Access to CloudKit Web Services
Stars: ✭ 129 (+143.4%)
Mutual labels:  vapor
Flock
Automated deployment of Swift projects to servers
Stars: ✭ 127 (+139.62%)
Mutual labels:  vapor
web-template
A starting point for web applications
Stars: ✭ 42 (-20.75%)
Mutual labels:  vapor
xcode-leaf-color-schemer
https://ashokgelal.com/2017/01/19/leaf_color_schemer_xcode/?ref=github
Stars: ✭ 26 (-50.94%)
Mutual labels:  vapor
Http
🚀 Non-blocking, event-driven HTTP built on Swift NIO.
Stars: ✭ 220 (+315.09%)
Mutual labels:  vapor
Websocket Kit
WebSocket client library built on SwiftNIO
Stars: ✭ 155 (+192.45%)
Mutual labels:  vapor
Swiftybot
How to create a Telegram, Facebook Messenger, and Google Assistant bot with Swift using Vapor on Ubuntu / macOS.
Stars: ✭ 247 (+366.04%)
Mutual labels:  vapor
Api Template
💧 A starting point for Vapor APIs.
Stars: ✭ 130 (+145.28%)
Mutual labels:  vapor
wkhtmltopdf-crystal
Crystal C bindings and wrapper for libwkhtmltox library
Stars: ✭ 21 (-60.38%)
Mutual labels:  wkhtmltopdf
Postgresql
Robust PostgreSQL interface for Swift
Stars: ✭ 127 (+139.62%)
Mutual labels:  vapor
Toolbox
Simplifies common command line tasks when using Vapor
Stars: ✭ 194 (+266.04%)
Mutual labels:  vapor
Console Kit
💻 APIs for creating interactive CLI tools.
Stars: ✭ 252 (+375.47%)
Mutual labels:  vapor

wkhtmltopdf

Swift Vapor Travis

Vapor 4 library for converting HTML (Leaf or otherwise) into PDF files using wkhtmltopdf.

Getting Started

Add the following in your Package.swift file

.package(url: "https://github.com/vapor-community/wkhtmltopdf.git", from: "4.0.0"),

📘 Overview

First, install wkhtmltopdf. This library is tested on version 0.12.5. Specify the location of wkhtmltopdf in the Document initialiser. The default is /usr/local/bin/wkhtmltopdf. Run it to ensure it and any dependencies are installed correctly.

To create a PDF, create and configure a Document, add one or more Pages, and then call generatePDF(on: threadPool, eventLoop: eventLoop). Here is a full example using Vapor:

import wkhtmltopdf

func pdf(_ req: Request) -> EventLoopFuture<Response> {
    // Create document. Margins in mm, can be set individually or all at once.
    // If no margins are set, the default is 20mm.
    let document = Document(margins: 15)
    // Create a page from an HTML string.
    let page1 = Page("<p>Page from direct HTML</p>")

    // Create a page from a Leaf template.
    let page2 = req.view.render("page_from_leaf_template")

    // Create a page from a Leaf template with Context variables.
    let context = ["firstName": "Peter", "lastName": "Pan"]
    let page3 = req.view.render("page_from_leaf_template", context)

    let pages = [ page2, page3]
        .flatten(on: req.eventLoop)
        .map { views in
            views.map { Page($0.data) }
        }

    return pages.flatMap { pages in
        // Add the pages to the document
        document.pages = [page1] + pages
        // Render to a PDF
        let pdf = document.generatePDF(on: req.application.threadPool, eventLoop: req.eventLoop)
        // Now you can return the PDF as a response, if you want
        return pdf.map { data in
            return Response(
                status: .ok,
                headers: HTTPHeaders([("Content-Type", "application/pdf")]),
                body: .init(data: data)
            )
        }
    }
}

In your Leaf file, you may want to load resources such as images, CSS stylesheets and web fonts. Store these in your Public directory, and you can direct wkhtmltopdf to this directory using the #(publicDir) tag.

If you'd like to use a non-public directory, you can use the #(workDir) tag to render the Droplet's working directory. Of course, you can always hard-code an absolute path instead.

Here is a worked example Leaf file which loads CSS and images. It uses the <base> tag to tell wkhtmltopdf to look in the Public directory by default.

<!DOCTYPE html>
<html>
  <head>
    <base href='#(publicDir)'>
    <link href='css/pdf.css' rel='stylesheet'>
  </head>
  <body>
    <img src='img/welcome.jpg'>
    <p>Welcome #(firstName) #(lastName)!</p>
  </body>
</html>

Zoom calibration

Across different platforms, wkhtmltopdf can require different zoom levels to ensure that 1 mm in HTML equals 1 mm in PDF. The default zoom level is 1.3, which has been found to work well on Linux, but if you need a different zoom level set the static property Document.zoom before doing any rendering.

Why Pages?

WebKit is not very good at rendering page breaks. If it works with your design, a good alternative is to split the PDF document into separate HTML files. wkhtmltopdf will combine them all and return a single PDF document.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].