我正在努力升级.NET应用程序而不更改其功能。到目前为止我所做的是:
现在我在自定义实用程序DLL中遇到了pdf转换。我得到的错误如下图所示。它说:
对于网址http://localhost:4984/Rijopdracht.mvc/Print/861175,ExportHtmlToPdfBytes失败:转换错误:WebKit导航超时。
该链接可在代理后面访问。 ExportHtmlToPdfBytes方法如下所示:
public byte[] ExportHtmlToPdfBytes(string url)
{
try
{
PdfConverter pdfConverter = CreatePdfConvertor();
byte[] bytes = pdfConverter.GetPdfBytesFromUrl(url);
return bytes;
}
catch (Exception ex)
{
throw new WrapperException(ex, "ExportHtmlToPdfBytes failed for url {0}:", url);
}
}
在调试过程中,我发现程序在调用函数GetPdfBytesFromUrl时会中断。程序中断后,我用来访问URL的代理用户被阻止。我发现,当且仅当密码输入错误6次或更多次时,用户才会被阻止。
我仔细检查了来自appsettings的用户名和密码,它们是正确的。有人能告诉我发生了什么以及如何解决这个问题吗?
此函数的完整类PdfUtilities是:
using ExpertPdf.HtmlToPdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jumbo.Utilities
{
public class PdfUtilities
{
public PdfUtilities() {
FooterText = String.Empty;
FitWidth = false;
LeftMargin = 5;
RightMargin = 5;
TopMargin = 5;
BottomMargin = 5;
PdfCompressionLevel = PdfCompressionLevel.Normal;
JpegCompressionLevel = 10;
}
public string FooterText { get; set; }
public string AuthenticationUsername { get; set; }
public string AuthenticationPassword { get; set; }
public bool FitWidth { get; set; }
public string BaseUrl { get; set; }
public int LeftMargin { get; set; }
public int RightMargin { get; set; }
public int TopMargin { get; set; }
public int BottomMargin { get; set; }
public int JpegCompressionLevel { get; set; }
public PdfCompressionLevel PdfCompressionLevel { get; set; }
private PdfConverter CreatePdfConvertor()
{
PdfConverter pdfConverter = new PdfConverter();
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel;
pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Landscape;
pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = false;
pdfConverter.PdfDocumentOptions.ShowHeader = true;
pdfConverter.PdfDocumentOptions.ShowFooter = true;
pdfConverter.PdfDocumentOptions.LeftMargin = LeftMargin;
pdfConverter.PdfDocumentOptions.RightMargin = RightMargin;
pdfConverter.PdfDocumentOptions.TopMargin = TopMargin;
pdfConverter.PdfDocumentOptions.BottomMargin = BottomMargin;
pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
pdfConverter.PdfDocumentOptions.EmbedFonts = true;
pdfConverter.PdfDocumentOptions.JpegCompressionLevel = JpegCompressionLevel;
if (FitWidth)
{
pdfConverter.PageWidth = 0;
pdfConverter.PdfDocumentOptions.FitWidth = true;
pdfConverter.PdfDocumentOptions.StretchToFit = true;
}
else
pdfConverter.PdfDocumentOptions.FitWidth = false;
//pdfConverter.PageWidth = 0;
pdfConverter.PdfDocumentOptions.ShowHeader = false;
pdfConverter.PdfFooterOptions.FooterTextColor = System.Drawing.Color.Black;
pdfConverter.PdfFooterOptions.DrawFooterLine = false;
pdfConverter.PdfFooterOptions.PageNumberText = "Page";
pdfConverter.PdfFooterOptions.ShowPageNumber = true;
pdfConverter.PdfFooterOptions.FooterText = FooterText;
if (!string.IsNullOrEmpty(AuthenticationUsername) && !string.IsNullOrEmpty(AuthenticationPassword))
{
pdfConverter.AuthenticationOptions.Username = AuthenticationUsername;
pdfConverter.AuthenticationOptions.Password = AuthenticationPassword;
}
else
{
string username = SettingsUtilities.GetSetting<string>(GenericConstants.AS_PROXY_USERNAME, null);
if (!String.IsNullOrEmpty(username))
{
string password = SettingsUtilities.GetSetting<string>(GenericConstants.AS_PROXY_PASSWORD, null);
pdfConverter.AuthenticationOptions.Username = username;
pdfConverter.AuthenticationOptions.Password = password;
}
}
pdfConverter.LicenseKey = "tZ6HlY2Vh4WBg5WDm4WVhoSbhIebjIyMjA==";
return pdfConverter;
}
public byte[] ExportHtmlToPdfBytes(string url)
{
try
{
PdfConverter pdfConverter = CreatePdfConvertor();
byte[] bytes = pdfConverter.GetPdfBytesFromUrl(url);
return bytes;
}
catch (Exception ex)
{
throw new WrapperException(ex, "ExportHtmlToPdfBytes failed for url {0}:", url);
}
}
public byte[] ExportHtmlStringToPdfBytes(string html)
{
PdfConverter pdfConverter = CreatePdfConvertor();
byte[] bytes;
if (BaseUrl == null)
bytes = pdfConverter.GetPdfBytesFromHtmlString(html);
else
bytes = pdfConverter.GetPdfBytesFromHtmlString(html, BaseUrl);
return bytes;
}
public void SaveHtmlToPdf(string url, string saveLocation)
{
Logging.LogVerboseCaller(Logging.Cat.Utilities, "Start export to htm for " + url);
byte[] contents = ExportHtmlToPdfBytes(url);
Logging.LogVerboseCaller(Logging.Cat.Utilities, "End export. Start save to " + saveLocation);
FileUtilities.SaveFile(saveLocation, contents);
Logging.LogVerboseCaller(Logging.Cat.Utilities, "End save");
}
}
}
答案 0 :(得分:0)
我的一位同事能够解决它。
首先:如果我们需要代理,我们就不应该使用AuthenticationOptions。我相信这些是用您的ExpertPDF帐户登录的。 PdfConverter类有代理属性可用于此。
但是,这不是问题所在。我的问题是我使用了早期版本的许可证。该错误并不清楚,但它是一个无效的许可证密钥。
为了测试这一点,我们注释掉了许可证密钥。我们自动使用了在生成的pdf上添加水印的演示版本,但它告诉我们这是一个错误的许可证密钥,搞砸了它。