Coder Social home page Coder Social logo

wkwrap's Introduction

WkWrap

wkhtmltopdf cross-platform C# wrapper for .NET Core.

Overview

This project is build against .NET Standard 1.4 using the RTM tooling that ships with Visual Studio 2017. This is the only configuration I'm support on the issue tracker. WkWrap calls wkhtmltopdf in stream-based processing mode, so you don't need to provide any temp folders.

Getting started

Installation

The current version of the library is available on NuGet.

Install-Package WkWrap.Core

Simple usage

WkWrap is designed as easy to use wkhtmltopdf cross-platform wrapper. To start using it you need to install wkhtmltopdf on your system and create instance of HtmlToPdfConverter class. One thing you need to do is provide path to wkhtmltopdf executable file (may require chmod+x on * nix based systems).

var wkhtmltopdf = new FileInfo(@"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe");
var converter = new HtmlToPdfConverter(wkhtmltopdf);
var pdfBytes = converter.ConvertToPdf(html);

Customize processing

If you want to customize processing options you can do it with providing custom ConversionSettings class instance.

var wkhtmltopdf = new FileInfo(@"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe");
var converter = new HtmlToPdfConverter(wkhtmltopdf);
var settings = new ConversionSettings(
    pageSize: PageSize.A3,
    orientation: PageOrientation.Landscape,
    margins: new PageMargins(0, 0, 0, 0),
    grayscale: true,
    lowQuality: true,
    quiet: true,
    enableJavaScript: true,
    javaScriptDelay: null,
    enableExternalLinks: true,
    enableImages: true,
    executionTimeout: null);
var pdfBytes = converter.ConvertToPdf(html, Encoding.UTF8, settings);

Stream-based processing

If you need to convert html in stream mode you can do it with specific stream-based HtmlToPdfConverter instance method.

var resultStream = new MemoryStream();
var wkhtmltopdf = new FileInfo(@"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe");
var converter = new HtmlToPdfConverter(wkhtmltopdf);
converter.ConvertToPdf(htmlMemorySream, resultStream, ConversionSettings.Default());

Custom wkhtmltopdf options

Instance of ConversionSettings just converts to wkhtmltopdf command line arguments with .ToString() method call. So if you want to specify custom wkhtmltopdf command line arguments you can do it too!

var resultStream = new MemoryStream();
var wkhtmltopdf = new FileInfo(@"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe");
var converter = new HtmlToPdfConverter(wkhtmltopdf);
converter.ConvertToPdf(htmlMemorySream, resultStream, "-s A3 -L 0 -T 0 -B 0 -R 0 -g -q");

Or you can call wkhtmltopdf with default options using

var resultStream = new MemoryStream();
var wkhtmltopdf = new FileInfo(@"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe");
var converter = new HtmlToPdfConverter(wkhtmltopdf);
string settings = null;
converter.ConvertToPdf(htmlMemorySream, resultStream, settings);

wkwrap's People

Contributors

nomoreload avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.