2

I'm working in a website with PDF generation, but the result changes between the local execution and the final deployed site (the site effectively generates the PDF and the same binaries were checked with dotnet run -c Release and the result is the same in localhost Release and localhost Debug).

I looked for some information or specific instructions for deployment, these are the official docs from https://selectpdf.com/html-to-pdf/docs/html/Deployment.htm but it doesn't have something special, I already made bigger modifications and I don't know if this is something like a config problem.

This is the function for Pdf Rendering

private Byte[] GetPdf(DocumentViewModel model)
{
    var url = "a simple URL";
    var converter = new HtmlToPdf();
    converter.Options.CssMediaType = HtmlToPdfCssMediaType.Screen;
    converter.Options.JavaScriptEnabled = true;
    converter.Options.MarginBottom = 0;
    converter.Options.MarginRight = 0;
    converter.Options.MarginLeft = 0;
    converter.Options.MarginTop = 0;
    converter.Options.PdfPageSize = PdfPageSize.Letter;
    converter.Options.AutoFitWidth = HtmlToPdfPageFitMode.AutoFit;
    converter.Options.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
    converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape;
    converter.Options.MinPageLoadTime = 2;
    converter.Options.MaxPageLoadTime = 30;
    var doc = converter.ConvertUrl(url);
    using (var stream = new MemoryStream())
    {
         doc.Save(stream);
         return stream.ToArray();
    }
}

My web.config doesn't have anything particularly special, but here it is

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\WebSite.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
   <staticContent>   
    <remove fileExtension=".pdf" />         
     <mimeMap fileExtension=".pdf" mimeType="application/pdf" />        
   </staticContent>
    </system.webServer>
  </location>
    <system.web>
        <sessionState timeout="60" />
    </system.web>
    <system.webServer>
        <caching>
            <profiles>
                <add extension=".pdf" policy="DisableCache" kernelCachePolicy="DisableCache" />
            </profiles>
        </caching>
    </system.webServer>
</configuration>

With the current method, my results are different from local to server. I need to set the placement of elements in the website but I can't if I don't know which viewport is selected.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Erick IO
  • 46
  • 7
  • Could you say how the results differ based on some input data? That would be really helpful. What platform are you running locally, and what platform are you running on the server? – Pavisa May 11 '19 at 07:06
  • Windows 10 is the dev platform, and WinServer 2012 is the Deployment platform. The webpage and for instance, the final PDF looks well in the dev stage, but looks like the viewport is smaller in server, or something like that – Erick IO May 11 '19 at 20:53
  • Hello Erick, did you find a solution or a workaround? I'm facing the same problem now! – nemenos Jun 09 '20 at 06:54

1 Answers1

1

Content is resized during the conversion to pdf: https://selectpdf.com/docs/ResizingContentDuringConversion.htm

Try to change the viewport width to see what happens:

converter.Options.WebPageWidth = 1024;

Try to set some other values (depending on your web page width): 800, 1920 (these are in pixels).

Tom
  • 285
  • 2
  • 3