Pdf导出的操作:引用TuesPechkin.dll和TuesPechkin.Wkhtmltox.AnyCPU.dll程序集,新建PDF静态类 PDFConverter,在web.config配置保存dir
///
///pdf转换///
public static classPdfConvert {///
///staticDeploymentPath///
private static readonly string StaticDeploymentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wkhtmltopdf");///
///CreateWkhtmltopdfPath///
public static voidCreateWkhtmltopdfPath() {if (Directory.Exists(StaticDeploymentPath) == false) {
Directory.CreateDirectory(StaticDeploymentPath);
}
}///
///converter///
public static IConverter Converter =
newThreadSafeConverter(new RemotingToolset(newWinAnyCPUEmbeddedDeployment(newStaticDeployment(StaticDeploymentPath)
)
)
);
}
publicHttpResponseMessage CreateFormPdf(WebTaskFormDetail wtData) {try{string fileName = AesCode.AesDecrypt(wtData.TaskId) + "_" +wtData.FormId;var baseUrl =GetBaseUrl();string url = baseUrl +UrlFormTemplate;
HttpClient client= newHttpClient();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,refpdfBuf);var downloadUrl = new Uri(baseUrl + ZxjcAttachmentDir.Replace(@"\", "/") + "pdf/" + fileName + ".pdf");//关联任务附件
var attachmentInfo = newAttachmentInfo {
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(newMemoryStream(pdfBuf)) };
response.Content.Headers.ContentDisposition= new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentLocation=downloadUrl;
response.Content.Headers.ContentType= new MediaTypeHeaderValue("application/pdf");returnresponse;
}catch(Exception ex) {throw;
}
}private void CreatePdf(string path, string fileName, ref byte[] pdfBuf) {var document = newHtmlToPdfDocument {
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 stringPhysicalApplicationPath() {returnHttpContext.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);
}