title: How to convert a HTML File to PDF using WkHTMLToSharp / wkhtmltopdf with Images in C#
body: <p>I am generating HTML files on the fly, and I would like to create a PDF from the final file.  I am using the following to generate the HTML file:</p>
  
  <pre><code>    public static void WriteHTML(string cFile, List&lt;Movie&gt; mList)
      {
          int lineID = 0;
          string strHeader, strMovie, strGenre, tmpGenre = null;
  
          string strPDF = null;
  
          // initiates streamwriter for catalog output file
          FileStream fs = new FileStream(cFile, FileMode.Create);
          StreamWriter catalog = new StreamWriter(fs);
  
          strHeader = "&lt;style type=\"text/css\"&gt;\r\n" + "&lt;!--\r\n" + "tr#odd {\r\n" + "   background-color:#e2e2e2;\r\n" + "  vertical-align:top;\r\n" + "}\r\n" + "\r\n" + "tr#even {\r\n" + "   vertical-align:top;\r\n" + "}\r\n" + "div#title {\r\n" + "  font-size:16px;\r\n" + "    font-weight:bold;\r\n" + "}\r\n" + "\r\n" + "div#mpaa {\r\n" + "    font-size:10px;\r\n" + "}\r\n" + "\r\n" + "div#genre {\r\n" + " font-size:12px;\r\n" + "    font-style:italic;\r\n" + "}\r\n" + "\r\n" + "div#plot {\r\n" + "   height: 63px;\r\n" + "  font-size:12px;\r\n" + "    overflow:hidden;\r\n" + "}\r\n" + "--&gt;\r\n" + "&lt;/style&gt;\r\n" + "\r\n" + "&lt;html&gt;\r\n" + "    &lt;body&gt;\r\n" + "     &lt;table&gt;\r\n";
          catalog.WriteLine(strHeader);
          strPDF = strHeader;
  
          foreach (Movie m in mList)
          {
              tmpGenre = null;
  
              strMovie = lineID == 0 ? "          &lt;tr id=\"odd\" style=\"page-break-inside:avoid\"&gt;\r\n" : "          &lt;tr id=\"even\" style=\"page-break-inside:avoid\"&gt;\r\n";
              catalog.WriteLine(strMovie);
              strPDF += strMovie;
  
              foreach (string genre in m.Genres)
                  tmpGenre += ", &lt;a href=\"" + genre + ".html\" target=\"_blank\"&gt;" + genre + "&lt;/a&gt;";
              strGenre = tmpGenre != null ? tmpGenre.Substring(2) : null;
  
              strMovie = "                &lt;td&gt;\r\n" + "                   &lt;img src=\".\\images\\" + m.ImageFile + "\" width=\"75\" height=\"110\"&gt;\r\n" + "               &lt;/td&gt;\r\n" + "              &lt;td&gt;\r\n" + "                   &lt;div id=\"title\"&gt;" + m.Title + "&lt;/div&gt;\r\n" + "                    &lt;div id=\"mpaa\"&gt;" + m.Certification + " " + m.MPAA + "&lt;/div&gt;\r\n" + "                  &lt;div id=\"genre\"&gt;" + strGenre + "&lt;/div&gt;\r\n" + "                   &lt;div id=\"plot\"&gt;" + m.Plot + "&lt;/div&gt;\r\n" + "              &lt;/td&gt;\r\n" + "          &lt;/tr&gt;\r\n";
              catalog.WriteLine(strMovie);
              strPDF += strMovie;
              lineID = lineID == 0 ? 1 : 0;
          }
  
          string closingHTML = "      &lt;/table&gt;\r\n" + "   &lt;/body&gt;\r\n" + "&lt;/html&gt;";
          catalog.WriteLine(closingHTML);
          strPDF += closingHTML;
          WritePDF(strPDF, cFile + ".PDF");
          catalog.Close();
      }
  </code></pre>
  
  <p>Once completed, I want to call the following function to generate the PDF file:</p>
  
  <pre><code>public static void WritePDF(string cFile, string pdfFile)
  {
      WkHtmlToPdfConverter w = new WkHtmlToPdfConverter();
  
      byte[] strHTML = w.Convert(cFile);
      File.WriteAllBytes(pdfFile, strHTML);
      w.Dispose();
  }
  </code></pre>
  
  <p>I've discovered that the .Convert function will convert HTML code to PDF, not a file.  Secondly, when I pass in the HTML code directly, the images are not appearing in the PDF.  I know there is an issue with .GIF files, but these are all .JPG files.</p>
  
  <p>I've read a lot about how good wkhtmltopdf is, and the guy who wrote WkHTMLToSharp posted his project all over SO, but I've been disappointed by the lack of documentation for it.</p>
  
  <p>I WANT to be able to pass in a file to convert, change the margins (I know this is possible, I just need to figure out the correct settings), have it convert images correctly, and most importantly, to not break up my items across multiple pages (support "page-break-inside:avoid" or something similar).</p>
  
  <p>I'd love to see how others are using this!</p>