preface

Good things to share, has been in the use of WkHTMLTopDF PDF file generation, the common way is to install wKHTMLTopDF, and then in the program with the command will be the corresponding HTML PDF file generation, simple and convenient; But repeated coding makes want to wkHTMLtopdf on the basis of encapsulation, accidentally found that a small partner has encapsulated well, commonly used functions have been implemented, source address: github.com/fpanaccia/W…

The author packaged it into Nuget package (Wkhtmltopdf.NetCore), directly introduced to use;

The body of the

Wkhtmltopdf has always been used on Windows and has not been tried on other platforms. If this package is good enough, pull it out for a walk. Let’s try it out:

1. Build an API project, introduce the package and compatible wKHTMLTopdf execution file of the corresponding platform;

Note: The default dependent wkHTMLTopdf execution file needs to be stored in the Rotativa directory. You can customize the name. If you customize the name, specify the corresponding file name when you need to register the service again. Here wkHTMLTopdf has been compiled and packaged according to different platforms, there is no need to install, these files are available in the source code;

2. Create PDFTestController and add the following interfaces for testing.

First inject the service that generates PDF, then use it directly:

The next step is to write the interface. This is just testing, and the code redundancy is not considered. Small partners can encapsulate according to their own needs in the actual project.

  • ExportPDFByHtml interface, using HTML to directly generate PDF files, but there is no save, in the form of file flow access, through the browser to view the file, you can download; HTML templates can be stored as separate files in the actual development process.

  • SavePDFByHtml interface, save files directly, file name can be customized according to need;

  • TestMarginAndPageSize interface, set Margin and PageSize parameters, other parameters can also be set;

    ConvertOptions encapsulates the following attributes by default. Partners can also customize the extension, as long as they inherit IConvertOptions, which will not be demonstrated here, because there are official cases for partners to work with, wkhtmltopdf parameters are quite a lot, can be encapsulated and used.

  • ExportByRazorView uses Razor views to generate PDF files. The library already supports reading CSHTML files

    Generate the corresponding PDF effect according to the specified view, as follows:

  • ExportByRazorViewData data dynamic binding, since support view, then should support Razor syntax, generally used is data binding, above is static, next to a dynamic binding.

    Generate the corresponding PDF effect according to the specified view, as follows:

    The above basic use demo said so much, the use is still very simple, small partners can follow up according to their own needs related extension; Of course, there are other functions, such as setting the header/footer, etc., the author provides corresponding cases; Here do not say so much, otherwise it will be long.

3. Problems that may be encountered by small partners when using it

  • The wkhtmltopdf file cannot be found during the development and debugging of the project. That is because the runtime does not find the corresponding file. Set the corresponding file under Rotativa to always copy:

  • I’m using centos 7. I’m using centos 7. I’m using centos 7.

    See this wrong I meng, a search fierce as tiger, or did not find the answer; Calm down and try again. You are being stupid.

    Two problems need to be solved: 1. Wkhtmltopdf uploaded to Linux does not give execution permission; 2. 2. The environment may lack the corresponding dependent library;

    Set the executable permission

    In Linux, you can run ll to check the permission. You do not have the permission at first. You only need to run chmod 777 wkhtmltopdf to obtain the permission.

    Install the missing dependency libraries

    After the executable permission is enabled, don’t rush to the page, it may still be wrong. Wkhtmltopdf, wkhtmlTopdf, wkhtmltopdf, wkhtmltopdf, wkhtmltopdf, wkhtmltopdf Try./wkhtmltopdf https://www.baidu.com./test. PDF, because wkhtmltopdf itself can run on its own, without relying on the program we wrote.

  • When the execution is successful, then start to access the interface export function, if there is no accident, encountered Chinese characters will generate gibberish, that is because the Linux environment is lack of relevant font files, the corresponding font files can be copied to Linux, I have found the font, download address is as follows:

    Link: pan.baidu.com/s/1jikC0DUk… Extraction code: TN4j;

    Unzip the downloaded fonts and copy them to /usr/share/fonts on Linux

Finally this should be no problem, the rest of the partner to explore their own practice it;

This article source address: github.com/zyq025/DotN…

Wkhtmltopdf official website address: wkhtmltopdf.org/

conclusion

It is very simple to use, the conventional requirements have no problem, if you need to customize the function, small partners can refer to the source code, their own packaging (packaging ideas is not difficult); If your partner has a good export library, free and open source, share it with you.

A handsome guy who was made ugly by the program, watch “Code variety circle “with me to learn ~~~