Pdf导出的操作:引用TuesPechkin.dll和TuesPechkin.Wkhtmltox.AnyCPU.dll程序集,新建PDF静态类 PDFConverter,在web.config配置保存dir
/// <summary> /// pdf转换 /// </summary> public static class PdfConvert { /// <summary> /// staticDeploymentPath /// </summary> private static readonly string StaticDeploymentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wkhtmltopdf"); /// <summary> /// CreateWkhtmltopdfPath /// </summary> public static void CreateWkhtmltopdfPath() { if (Directory.Exists(StaticDeploymentPath) == false) { Directory.CreateDirectory(StaticDeploymentPath); } } /// <summary> /// converter /// </summary> public static IConverter Converter = new ThreadSafeConverter( new RemotingToolset<PdfToolset>( new WinAnyCPUEmbeddedDeployment( new StaticDeployment(StaticDeploymentPath) ) ) ); }
public HttpResponseMessage CreateFormPdf(WebTaskFormDetail wtData) { try { string fileName = AesCode.AesDecrypt(wtData.TaskId) + "_" + wtData.FormId; var baseUrl = GetBaseUrl(); string url = baseUrl + UrlFormTemplate; HttpClient client = new HttpClient(); var template = client.GetStringAsync(url).Result; string page = "form.html"; url = baseUrl + UrlFormContentDir + page; var content = client.GetStringAsync(url).Result; var json = JsonConvert.SerializeObject(wtData).Replace(@"", @"\"); string html = template.Replace("[[content]]", content).Replace("[[json]]", json); string path = HttpContext.Current.Request.PhysicalApplicationPath + ZxjcAttachmentDir + @"html"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } StreamWriter sw = new StreamWriter(path + fileName + ".html", false, Encoding.UTF8); sw.Write(html); sw.Close(); path = ZxjcAttachmentDir + @"pdf"; if (!Directory.Exists(Path.Combine(PhysicalApplicationPath(), path))) { Directory.CreateDirectory(Path.Combine(PhysicalApplicationPath(), path)); } var savePath = path + fileName + ".pdf"; byte[] pdfBuf = null; CreatePdf(Path.Combine(PhysicalApplicationPath(), savePath), fileName, ref pdfBuf); var downloadUrl = new Uri(baseUrl + ZxjcAttachmentDir.Replace(@"", "/") + "pdf/" + fileName + ".pdf"); //关联任务附件 var attachmentInfo = new AttachmentInfo { AttachmentName = wtData.FormName + ".pdf", Path = savePath, Url = downloadUrl.ToString(), Description = wtData.Description }; SaveAttachment(attachmentInfo,Convert.ToInt32(AesCode.AesDecrypt(wtData.TaskId))); HttpResponseMessage response = new HttpResponseMessage { Content = new StreamContent(new MemoryStream(pdfBuf)) }; response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentLocation = downloadUrl; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); return response; } catch (Exception ex) { throw; } } private void CreatePdf(string path, string fileName, ref byte[] pdfBuf) { var document = new HtmlToPdfDocument { GlobalSettings ={ ProduceOutline = true, DocumentTitle = "web 表单", PaperSize = PaperKind.A4, Margins ={All = 1.375,Unit = Unit.Centimeters} //Margins = new MarginSettings(50,50,50,50) }, Objects = { new ObjectSettings { PageUrl = this.GetBaseUrl() + ZxjcAttachmentDir.Replace(@"", "/") + "html/" + fileName + ".html" } } }; pdfBuf = PdfConvert.Converter.Convert(document); File.WriteAllBytes(path, pdfBuf); } private static string PhysicalApplicationPath() { return HttpContext.Current.Request.PhysicalApplicationPath; }
直接在浏览器链接打印机打印
private void CreatePdf(string path, string fileName, ref byte[] pdfBuf) { var pageUrl = this.GetBaseUrl() + ZxjcAttachmentDir.Replace(@"", "/") + "html/" + fileName + ".html"; var document = new HtmlToPdfDocument { GlobalSettings ={ ProduceOutline = true, Copies=1, DPI = 1200, DocumentTitle = "web 表单", PaperSize = PaperKind.A4, Orientation = GlobalSettings.PaperOrientation.Portrait, OutputFormat = GlobalSettings.DocumentOutputFormat.PDF, Margins ={All = 1.375,Unit = Unit.Centimeters} //Margins = new MarginSettings(50,50,50,50) }, Objects = { new ObjectSettings { PageUrl = pageUrl, //HtmlText = docHtml, WebSettings = new WebSettings { DefaultEncoding = "utf-8", LoadImages =true, PrintBackground =true, EnableJavascript =true , PrintMediaType =true, EnablePlugins = true }, //LoadSettings =new LoadSettings { // BlockLocalFileAccess = false, // RenderDelay=5000, //} } } }; pdfBuf = PdfConvert.Converter.Convert(document); File.WriteAllBytes(path, pdfBuf); }