0

I develop an intranet website. And i want to use wkhtmltopdf to generate a pdf from a page of my website. After some researches, i found wkhtmltopdf. The application works great. But i'm new in C# and even i read Calling wkhtmltopdf to generate PDF from HTML i cannot work this code.

EDIT

Here is the code, it seems i have an error (The directory name is invalid) at p.Start(); :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.Hosting;
using System.Diagnostics;
using System.IO;

public partial class _zipdownload : System.Web.UI.Page  {

protected void DoDownload(object sender, EventArgs e)
{
    var url = "http://dlp-wdi/TR/view_I.asp?ID=11080";
    var file = WKHtmlToPdf(url);
    if (file != null)
    {
        Response.ContentType = "Application/pdf";
        Response.BinaryWrite(file);
        Response.End();
    }
}

public byte[] WKHtmlToPdf(string url)
{
    var fileName = " - ";
    var wkhtmlDir = "bin\\wkhtmltopdf\\";
    var wkhtml = "bin\\wkhtmltopdf\\wkhtmltopdf.exe";
    var p = new Process();

    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.FileName = wkhtml;
    p.StartInfo.WorkingDirectory = wkhtmlDir;

    string switches = "";
    switches += "--print-media-type ";
    switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
    switches += "--page-size Letter ";
    p.StartInfo.Arguments = switches + " " + url + " " + fileName;
    p.Start();

    //read output
    byte[] buffer = new byte[32768];
    byte[] file;
    using(var ms = new MemoryStream())
    {
        while(true)
        {
            int read =  p.StandardOutput.BaseStream.Read(buffer, 0,buffer.Length);

            if(read <=0)
            {
                break;
            }
            ms.Write(buffer, 0, read);
        }
        file = ms.ToArray();
    }

    // wait or exit
    p.WaitForExit(60000);

    // read the exit code, close process
    int returnCode = p.ExitCode;
    p.Close();

    return returnCode == 0 ? file : null;
}
}

Thank you for your help.

Community
  • 1
  • 1
  • 1
    Tell us, what is going wrong. Any error message? – Ole Albers Aug 07 '14 at 09:37
  • I'm testing this code in my IIS and i have an "CS1518: Expected class, delegate, enum, interface, or struct" at the line where there is private void DoDownload() – DeadraNight Aug 07 '14 at 09:47
  • These are just methods. You must put them into a class. So add something like "class MyClass { // yourcode here } around it – Ole Albers Aug 07 '14 at 09:50
  • How silly of me! Thank you Ole Albers! Can i ask you a new question ? After some corrections, i have my button finaly visible but i have another error : System.ComponentModel.Win32Exception: The directory name is invalid at this line : p.Start(); – DeadraNight Aug 07 '14 at 10:18
  • You enter a relative path. Are you sure it exists where the program is executed? (Usually you are in a "bin\Debug" directory when a C# program is running. Try an absolute path. If that does not help: Ask a new question here in SO – Ole Albers Aug 07 '14 at 11:07
  • It works! Thank you so much Ole Albers! – DeadraNight Aug 08 '14 at 11:23
  • @OleAlbers Please add as answer so that this does not appear as unanswered in the wkhtmltopdf tag :) – Joel Peltonen Aug 15 '14 at 10:04

1 Answers1

0

These are just methods. You must put them into a class. So add something like "class MyClass { // yourcode here } around it

Ole Albers
  • 8,715
  • 10
  • 73
  • 166