Skip to content

bambams/WkHtmlToPdfDflat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

WkHtmlToPdfDflat is a work in progress .NET class library written in D♭
*errm* C# with .NET bindings for the wkhtmltopdf library. Currenty the
project is a Visual Studio 2010 project (I know, ugh), but eventually I
might get around to turning it into a command-line interface-friendly,
Mono-based project... Assuming Mono supports P/Invoke and what not...

The library doesn't seem to work properly right now. I haven't figured out
what is wrong. If you try to retrieve strings (e.g., phase description or
progress string) then you will get access violation exceptions. Otherwise,
the PDF seems to come out blank. In previous stages of this library you'd
get access violation exceptions from within the native convert function...
I don't know what is wrong, but clearly I'm not invoking the native
library properly somewhere.

Any help is appreciated. :)

== Windows ==

To use the library you'll need a copy of the wkhtmltox0.dll somewhere in
your PATH environment variable (or current working directory) so that
DllImportAttribute and P/Invoke can find it.  You can download precompiled
binaries from the Google Code project site:
<http://code.google.com/p/wkhtmltopdf/>. Or if you're brave enough then
you can attempt to build it yourself (I think it involves Qt, and in
Windows that sounds like a nightmare to me).

== Other OS ==

I'm not going to speculate on what you need for Linux yet.

== API ==

This describes the way the .NET API was originally meant to be used. I'm
not sure whether or not the semaphore is necessary (since the wkhtmltopdf
documentation doesn't seem to say whether or not the convert call blocks
until the conversion is complete (and all converter events have been
processed).

=== High-level Worker Example ===

using System;
using System.IO;
using System.Threading;
using WkHtmlToPdfDflat;

namespace Application
{
    class Program
    {
        static int Main(string[] args)
        {
            return new Program().Run(args);
        }

        protected int progress_ = 0;
        protected int status_ = 0;

        public int Run(string[] args)
        {
            using(var sem = new Semaphore(0, 1))
            {
                using(var worker = new Worker())
                {
                    worker.Error += (sender, e) => {
                        this.status_++;

                        Console.Error.WriteLine(e.String);

                        if(sem != null)
                            sem.Release();
                    };

                    worker.Finished += (sender, e) => {
                        using(var fs = new FileStream(
                                "output.pdf",
                                FileMode.Create))
                        {
                            var output = e.Converter.GetOutput();

                            fs.Write(output, 0, output.Length);
                        }

                        Console.WriteLine("Finished!");

                        if(sem != null)
                            sem.Release();
                    };

                    worker.PhaseChanged += (sender, e) => {
                        this.PrintProgress(e.Converter);
                    };

                    worker.ProgressChanged += (sender, e) => {
                        this.PrintProgress(e.Converter, e.Value);
                    };

                    worker.Warning += (sender, e) => {
                        this.status_++;

                        Console.Error.WriteLine(e.String);
                    };

                    var html = File.ReadAllText("example.html");

                    worker.Convert(html, null);

                    if(sem.WaitOne(30000))
                    {
                        throw new Exception(
                                "WkHtmlToPdfDflat never woke me up!");
                    }
                }
            }

            return this.status_;
        }

        protected void PrintProgress(
                Converter converter,
                int? progress = null)
        {
            if(progress != null)
                this.progress_ = (int)progress;

            Console.WriteLine(
                    "{0} - {1} - {2}%",
                    converter.CurrentPhaseDescription,
                    converter.ProgressString,
                    this.progress_);
        }
    }
}

=== Lower-level Converter Example ===

The Converter isn't initially meant to be used directly. If you choose to
do it then you will need to make some native calls yourself (or add some
more abstractions to the library). This might change if I can ever
actually get the library working at all. ;P

About

A C#/.NET class library with .NET bindings for wkhtmltopdf.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages