1, Use background
      Recently, the company needs tohtml Page content generationpdf And download, I tried many methods, but they didn't have satisfactory results, Later foundwkhtmltopdf This software, It finally solved the problem.

     wkhtmltopdf yesexe file, Download and install required, After that, all the files will be put intomvc project, useC# OfProcessStartInfo Call execution.

      Exportedhtml page:



2, Download and install
      Official website:https://wkhtmltopdf.org/ <https://wkhtmltopdf.org/>.

      homepagedownload Download the version of the corresponding system and install it invs After calling it convenient.


3, Parameters used

      wkhtmltopdf There are four parameters of: Cover object, Directory object, Document and page objects. Document object and page object are needed here.


      There are many other parameters, See official documents for specific parameter list:https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

      Usage method:wkhtmltop
      df [GLOBAL OPTION]... [OBJECT]... <output file>


      From left to right: Command start, Use grayscale mode, Disable smart scaling, Set header tohtml file, generatepdf Web address of, Generatedpdf File name.
      This is how you use it at the command prompt, staymvc Use in:
1 ProcessStartInfo startInfo = new ProcessStartInfo(); 2 startInfo.FileName =
file;3 startInfo.Arguments = "wkhtmltopdf --grayscale --disable-smart-shrinking
--header-html head.html https://www.baidu.com 123.pdf"; 4
startInfo.CreateNoWindow =true; 5 startInfo.WindowStyle =
ProcessWindowStyle.Hidden;6 var cc = Process.Start(startInfo); 7
cc.WaitForExit();8 cc.Close(); View Code

4, Add watermark
  Generated as required by the companypdf Need to add watermark, each pagepdf Show twologo Watermark image. For eachdiv Add watermark by setting background picture, Let the background picturey Shaft repetition. Because eachdiv The height of, Causes a watermark to be generated in twopdf page, Impact Aesthetics, Give up decisively. Use header settings laterhtml To display watermarks, What we should pay attention to here is, Header will occupyhtml Location, So I usebefore andafter Pseudo elements to write twologo watermark.

Watermark rendering:

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <style> 5 * { margin:0;padding:0;}
6 #divd::before{content:"";width:400px;height:400px;background:url(mark.png);
position:absolute;opacity:0.3;left:50%;margin-left:-200px;} 7 #divd::after{
opacity:0.3;left:50%;margin-left:-200px;top:400px} 8 </style> 9 </head> 10 <
bodystyle="border:0; margin: 0;"> 11 <div id="divd" style=""></div> 12 </body>
13 </html> View Code

After that, just use--header-html head.html This page will be called every pagepdf Generate two watermark images.


5,pdf paging
defaultpdf Generation is to write content from top to bottom, Not good-looking, Derivedpdf Effect:


The company requires eachdiv Need to start with a new page, Here is the page to be pageddiv Add topage-break-before:left Style can be, Finalpdf Effect:


6, Other
     a, If there istd perhapsth Need to hideborder, This style is requiredborder:0.
     b, Generatedpdf To set the spacingpadding No needmargin.