zoukankan      html  css  js  c++  java
  • 从HTML生成PDF文件

    从HTML生成PDF文件

    public static void WriterPDF(string tempFileName, bool isLowerWindows = true, bool withMarkwater = true)
    {
        string wkhtmltopdfPath = ResourceUtilities.WkhtmltopdfPathLower;
        if (!isLowerWindows)
        {
            wkhtmltopdfPath = ResourceUtilities.WkhtmltopdfPathHigher;
        }
    
        var pi = new ProcessStartInfo(wkhtmltopdfPath);
        pi.CreateNoWindow = true;
        pi.UseShellExecute = false;
        if (!Directory.Exists(ResourceUtilities.WkhtmltoxFolder))
        {
            Directory.CreateDirectory(ResourceUtilities.WkhtmltoxFolder);
        }
    
        if (!Directory.Exists(ResourceUtilities.PDFTempFolder))
        {
            Directory.CreateDirectory(ResourceUtilities.PDFTempFolder);
        }
    
        pi.WorkingDirectory = ResourceUtilities.WkhtmltoxFolder;
        string[] result = CreateWkhtmltopdfArguments(tempFileName, withMarkwater);
        pi.Arguments = result[0];
    
        using (var process = Process.Start(pi))
        {
            process.WaitForExit(99999);
            Debug.WriteLine(process.ExitCode);
        }
    
        if (withMarkwater)
        {
            string pdfExportFile = ExportFilePathHelper.GetExportFilePath() + tempFileName + ".pdf";
            AddWatermark(result[1], pdfExportFile);
            //IoHelper.DeleteFile(ResourceUtilities.PDFTempFolder + tempFileName + ".pdf");
            string htmlTempFolder = ResourceUtilities.HtmlTempFolder.Replace("\", "/");
            string htmlTempFile = htmlTempFolder + tempFileName + ".html";
            //IoHelper.DeleteFile(htmlTempFile);
        }
    }
    public static string[] CreateWkhtmltopdfArguments(string fileName, bool withMarkwater = false)
    {
    
        string arguments = "file:///";
        string htmlTempFolder = ResourceUtilities.HtmlTempFolder.Replace("\", "/");
        string htmlTempFile = htmlTempFolder + fileName + ".html";
        string pdfExportFolder = ExportFilePathHelper.GetExportFilePath();
        if (!Directory.Exists(pdfExportFolder))
        {
            Directory.CreateDirectory(pdfExportFolder);
        }
    
        string pdfExportFile = pdfExportFolder + fileName + ".pdf";
        if (withMarkwater)
        {
            string tempExportFolder = ResourceUtilities.PDFTempFolder;
            pdfExportFile = tempExportFolder + fileName + ".pdf";
        }
    
        arguments = arguments + htmlTempFile + " " + pdfExportFile;
        string[] result = new string[] { arguments, pdfExportFile };
        return result;
    
    }
    

    有几个地方要注意

    1. 使用的是wkhtmltopdf这个C++编写的工具
    2. 这个工具有两个主要的版本,一个是依赖VS2013 C++ Runtime编译的,一个是依赖早期的类库编译的,要根据自己的操作系统判断使用哪一个。官网地址http://wkhtmltopdf.org/downloads.html
  • 相关阅读:
    Protocol Buffer技术详解(语言规范)
    google protobuf 简单实例
    advStringGrid单元格文字垂直居中
    java中两个字符串如何比较大小
    List集合去除重复对象及equals()、hashCode()方法的作用
    Delphi中使用ActiveX的一些心得
    java List去重方式及效率对比
    Visual Studio Code 调整字体大小
    用最简单的例子实现jQuery图片即时上传
    Linux下绝对经典的命令
  • 原文地址:https://www.cnblogs.com/wuyicqb/p/5516359.html
Copyright © 2011-2022 走看看