title: WkHtmlToPDF - Working with stdin and headers/footers
body: <p>I'm trying to render an HTML document as PDF using wkhtmltopdf.exe, which I call from a C# web application.</p>
  
  <p>The HTML document needs to have both a footer and header that recur on every page, which is possible with wkhtmltopdf by specifying <code>--header-html &lt;a path&gt;</code> as an argument. </p>
  
  <p>However, the footer is rendered dynamically from a Razor view and I would rather not have to store it in a temporary file on disk and use that path, but I want to use the rendered HTML that's already in memory. That's possible for the document itself, by writing to the <code>StandardInput</code> stream, like so:</p>
  
  <pre><code>var wkhtml = ConfigurationManager.AppSettings["WkHtmlToPdfPath"];
  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.Arguments = "-q -n --disable-smart-shrinking - -";
  p.Start();
  
  var stdin = p.StandardInput;
  stdin.AutoFlush = true;
  stdin.Write(template);
  stdin.Dispose();
  </code></pre>
  
  <p>Is it possible to do the same thing for the header and footer HTML, namely to pass it in inline without having to resort to temporary files?</p>
  
  <p>I've tried:</p>
  
  <pre><code>stdin.Write(string.Format("--footer-html {0} ", footer));
  </code></pre>
  
  <p>But of course, it just treats that as part of the document, not a footer.</p>
  
  <p>The main reason I wanna render the footer and header dynamically as well is (mostly) caused by another issue. While it would be nice to have a dynamic header and footer, it's mostly to solve the problem that I have to link to images with an absolute path (ie: C:\templates\images\logo.png) because relative paths (ie: images/logo.png) don't work when you use stdin and just pass in a string blob of HTML, so I need to insert the absolute path through Razor at runtime.</p>
  
  <p>For this issue, I've tried setting the working directory of the process to match the relative paths, but to no avail:</p>
  
  <pre><code>p.StartInfo.WorkingDirectory = @"C:\templates";
  </code></pre>
  
  <p>If I could solve that problem, that would resolve 90% of the issue as well.</p>