How do I make the --viewport-size argument available in Tuespechkin?

765 Views Asked by At

I have a web application where I want to add a "Save as PDF" button on each page that when pressed will automatically convert the current web page to a PDF document available for download in the browser.

I have added TuesPechkin (v2.1.1) and TuesPechkin.Wkhtmltox.AnyCPU (v0.12.4.1) to my project references from NuGet. I got the PDF download working but it wasn't quite generating correctly since my site uses bootstrap (the PDF always exports in mobile view). I found that adding --viewport-size 1024x768 gives me the desired solution and was able to test this (outside the web app) with the latest wkhtmltopdf.exe (0.12.5) and the following command line:

wkhtmltopdf.exe --viewport-size 1024x768 -O Landscape -s A3 http://localhost/mywebpage output.pdf

When I try to find viewport-size argument in TuesPechkin, it is not available. This issue was created several years ago https://github.com/tuespetre/TuesPechkin/issues/122 and the author has described that it has not been added because the latest version of wkhtmltopdf (0.12.4) at the time, did not have that property available in it's API. Since then it has been added (0.12.5) https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2609

So... I cloned the TuesPechkin.Wkhtmltox.AnyCPU repo https://github.com/cratu/TuesPechkin. I downloaded the latest wkhtmltopdf 32bit and 64bit dll's. I used 7-Zip to add these to gz files and updated the files in the TuesPechkin.Wkhtmltox.AnyCPU repo. I then added the following code to the GlobalSettings.cs file:

[WkhtmltoxSetting("viewportSize")]
public string ViewportSize { get; set; }

I could then use set this property from my web application code:

var document = new HtmlToPdfDocument
{
    GlobalSettings =
    {
        DocumentTitle = this.Ets.Pages.Title,
        PaperSize = PaperKind.A3,
        Orientation = GlobalSettings.PaperOrientation.Landscape,
        ViewportSize = "1024x768"
    },
    Objects = {
        new ObjectSettings { PageUrl = this.Request.Url.AbsoluteUri }
    }
};

However, the PDF export from my web application still does not give me the desired result to match the output of the exe. The new ViewportSize property does not seem to have any affect at all. What am I missing?

Update

I created a .NET console application and the ViewportSize property is working. I checked the correct dll's are being used by the web app but the ViewportSize property still doesn't work like it does in the console app. I restarted IIS & the application pool but still the same result.

0

There are 0 best solutions below