首页 > 其他分享 >Html转换为PDF

Html转换为PDF

时间:2023-03-10 09:22:54浏览次数:32  
标签:pdfPath 转换 string return Html new var PDF properties

通过nuget 添加  text7.pdfhtml

 /// <summary>
        /// pdf下载
        /// </summary> 
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult DownloadPDF(string htmlContent)
        { 
            if (string.IsNullOrEmpty(htmlContent))
            {
                return null;
            } 
            var properties = new ConverterProperties();
            properties.SetCharset("utf-8");
            var provider = new DefaultFontProvider(true, true, true);// 第三个参数为True,以支持系统字体,否则不支持中文
            properties.SetFontProvider(provider);
            var mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);// 指当前设备类型,如果是预览使用SCREEN 
            properties.SetMediaDeviceDescription(mediaDeviceDescription);

            string pdfPath = Server.MapPath("~/" + DateTime.UtcNow.Ticks.ToString() + ".pdf");
            using (var fileInfo = new FileStream(pdfPath, FileMode.Create))
            {
                HtmlConverter.ConvertToPdf(htmlContent, fileInfo, properties);
            }
            string fileName = PublicVars.SchoolName + "准考证.pdf";
            //方式1:
            //return File(pdfPath, "application/octet-stream", HttpUtility.UrlEncode(fileName));

            //方式2:
            var fileContent = new FileContentResult(System.IO.File.ReadAllBytes(pdfPath), "application/octet-stream");
            fileContent.FileDownloadName = Server.UrlEncode(fileName);
            //下载完成删除文件
            System.IO.File.Delete(pdfPath);
            return fileContent;
        }

  

标签:pdfPath,转换,string,return,Html,new,var,PDF,properties
From: https://www.cnblogs.com/5tomorrow/p/17202244.html

相关文章

  • 解决苹果Safari 浏览器下html不能自动播放声音和视频的问题-实时语音通话功能【唯一客
    在实现我的客服系统中,实时语音通话功能的时候,如果想自动播放音视频流,在苹果设备上遇到了问题。苹果浏览器(Safari)在默认情况下不允许声音在背景里自动播放。这是出于用户体......
  • Java数据类型转换
    类型转换由于Java是强类型语言,所以要进行有些运算的时候需要用到类型转换。低 ---------------------------------> 高byte,short,char->int->long->float->doub......
  • list集合中String类型字段转换为BigDecimal类型求和
    List<User>userList=newArrayList<>();Useruser1=newUser();user1.setMoney("10.1");//double类型userList.add(user1);Useruser2=newUser();user2.set......
  • CAD表格怎么转换成Excel表格?CAD表格转EXCEL步骤
    无论是建筑行业还是机械行业,设计师在CAD制图过程中会将项目重要数据以表格的形式插入到图纸中,常见的CAD表格有明细表、工序卡、图样目录等。可是CAD表格在进行数据查找的时......
  • py文件转换为so文件
    将py文件编译为so文件    利用上面代码生成so文件生成的文件不方便取放,名字也不一样通过执行发现转换执行了下面三句/usr/local/python3/bin/cythontest.pyg......
  • vue中将base64流数据转成pdf文件可打印
    vue中将base64流数据转成pdf文件可打印这次后端返参不是oss那边传回来的,而是传给我一串base64编码的字符串,让我去生成可打印的pdf,返参如下所示。一、base编码转为pdf方......
  • # 工程坐标转换成cgcs2000坐标系
    工程坐标转换成cgcs2000坐标系在BIM模型中大多采用工程坐标,但是在前端进行实际位置渲染时,往往出现实际坐标系与工程坐标之间的转换困难问题。基于此,根据坐标系的生成原......
  • PDFJS 跨域
      https://cloud.tencent.com/developer/article/1531860?from=15425&areaSource=102001.1&traceId=B4KzYkzcjSz31MUNIH68v          ......
  • C# PDF 预览
    publicActionResultV1(){try{stringfilePath="E://123.pdf";if(System.IO.File......
  • ES6-ES11 bable对ES6模块化代码的转换(已过时,仅了解)
    原视频让ES新特性以ES5形式展现home.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-w......