Ham*_*eza 1 c# pdf pdf-generation html-to-pdf hiqpdf
我正在使用图像和css文件转换html HiQPdf 8.0
.我做过很多次了MVC 4
.现在我下工作MVC 5
,并HiQPdf 8.0
和问题HiQPdf 8.0
没有loadin外部文件如CSS文件和图像.我通过在页面中的样式标记中扭曲样式但不知道如何处理图像来解决css文件的问题.这是我的解决方案:
byte[] pdf = RenderPdf(contractId.Value, personId.Value, addressId.Value, transportId.Value, peacockeryNumber, peacockeryDate);
System.IO.File.WriteAllBytes(path, pdf);
public byte[] RenderPdf(int contractId, int personId, int addressId, int transportId, string peacockeryNumber, string peacockeryDate)
{
PdfPageViewModel model = new PdfPageViewModel();
model.Contract = contractRepo.GetContract(contractId);
model.Person = personRepo.GetPerson(personId);
model.Supplier = supplierRepo.GetSupplier(model.Contract.SupplierCode, 1);
model.Customer = customerRepo.GetCustomer(model.Contract.CustomerCode);
model.Address = customerRepo.GetAddress(addressId);
model.Transport = transportRepo.Detail(transportId);
model.Header = settingRepo.GetSetting(Model.Settings.SettingType.Header);
model.FooterAddress = settingRepo.GetSetting(Model.Settings.SettingType.Address);
model.Sign = settingRepo.GetSetting(Model.Settings.SettingType.Sign);
model.Stamp = settingRepo.GetSetting(Model.Settings.SettingType.Stamp);
model.PeacockeryDate = peacockeryDate;
model.PeacockeryNumber = peacockeryNumber;
string htmlToConvert = RenderPdfAsString("HtmlPdf", model);
String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl = thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertThisPageToPdf".Length);
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
htmlToPdfConverter.HiddenHtmlElements = new string[] { "#convertThisPageButtonDiv" };
byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);
byte[] b = pdfBuffer;
return b;
}
public string RenderPdfAsString(string viewName, object model)
{
PdfPageViewModel pdfPage = (PdfPageViewModel)model;
StringWriter stringWriter = new StringWriter();
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
new ViewDataDictionary(model),
new TempDataDictionary(),
stringWriter
);
viewResult.View.Render(viewContext, stringWriter);
return stringWriter.ToString();
}
Run Code Online (Sandbox Code Playgroud)
以下是HtmlPdf
观点:
<body id="prt-body" style="font-family: 'B Nazanin'">
<div class="prt-container">
<header id="prt-header">
<div id="prt-header-logo">
<img src="~/Images/logo.png" />
</div>
<ul id="prt-header-information">
<li><span>?????: </span>
<label>@Paristan.Broker.UI.Components.PersianDate.GetDate(DateTime.Now)</label></li>
<li><span>?????: </span>
<label>93323448/1</label></li>
<li><span>?????:</span><label>......................</label></li>
</ul>
<div class="clear"></div>
</header>
<div id="prt-main">
<ul id="prt-main-information">
<li><span>@Model.Person.Title</span><label></label></li>
<li><span>@Model.Person.Name</span><label></label></li>
<li><span>@Model.Supplier.Name</span></li>
</ul>
<div class="prt-main-message">@Html.Raw(Model.Header.Value.Replace("#Date", Model.Contract.ContractDate))</div>
<ul id="prt-main-tables">
<li>
<label>1) ?????? ????: </label>
<table class="prt-fiveTD">
<tr>
<th>??? ?????</th>
<th>????</th>
<th>????? ???? ???</th>
<th>????? ???? ???</th>
<th>????? ???? ??? ??</th>
</tr>
<tr>
<td>@Model.Contract.CommercialName</td>
<td>@Model.Contract.ProductSymbol</td>
<td>@Model.Contract.ContractDate</td>
<td>@Model.Contract.ContractNumber</td>
<td>@Model.Contract.ComponentNumber</td>
</tr>
</table>
</li>
<li>
<label>2) ?????? ??????: </label>
<table class="prt-fourTD">
<tr>
<th>??? ??????</th>
<th>?????????? ????????</th>
<th>?? ??????</th>
<th>????? ??? / ??? ???</th>
</tr>
<tr>
<td>@Model.Customer.Name</td>
<td>@Model.Customer.PetroChemicalCode</td>
<td>@Model.Customer.EconomyCode</td>
<td>@Model.Customer.RegisterLocation/@Model.Customer.RegisterNumber</td>
</tr>
</table>
<table class="prt-fourTD">
<tr>
<th>?????????? ???</th>
<th>?? ????</th>
<th>????</th>
<th>??? ??????? / ???? ???????</th>
</tr>
<tr>
<td>@Model.Customer.CodeMelli</td>
<td>@Model.Customer.PostalCode</td>
<td>@Model.Customer.PhoneNumber</td>
<td>@Model.Customer.DelegacyPhoneNumber/@Model.Customer.DelegacyName</td>
</tr>
</table>
<table class="prt-twoTD">
<tr>
<th>????</th>
</tr>
<tr>
<td>@Model.Customer.Address</td>
</tr>
</table>
</li>
<li>
<label>3) ?????? ???? ??? (????? ?? ????): </label>
<table class="prt-fiveTD">
<tr>
<th>??? ??????</th>
<th>??? (???????)</th>
<th>???? ????</th>
<th>???? ?? ????</th>
<th>?????? ????</th>
</tr>
<tr>
@{double totalAmount = int.Parse(Model.Contract.Amount) * int.Parse(Model.Contract.Price);
double wage = totalAmount * 0.00278;
double settlementAmount = totalAmount * 0.99722;
double tax = totalAmount * 0.08;}
<td>@Model.Contract.ContractType</td>
<td>@Model.Contract.Amount</td>
<td>@Model.Contract.Price</td>
<td>@totalAmount</td>
<td>@wage</td>
</tr>
</table>
<table class="prt-fourTD">
<tr>
<th>???? ????? ?? ???? ???????</th>
<th>?????? ?? ???? ?????? ??????</th>
<th>????? ??? ?????? vat</th>
<th>????? ??? ????? vat</th>
</tr>
<tr>
<td>@settlementAmount</td>
<td>@tax</td>
<td>@Model.PeacockeryNumber</td>
<td>@Model.PeacockeryDate</td>
</tr>
</table>
</li>
<li>
<label>4) ???????? ???: </label>
<table class="prt-twoTD">
<tr>
<th>??? ????????</th>
<th>?? ????????</th>
</tr>
<tr>
<td>@Model.Transport.Name</td>
<td>@Model.Transport.TransportCode</td>
</tr>
</table>
</li>
<li>
<label>5) ???? ??? ?????: </label>
<table class="prt-oneTD">
<tr>
<th>???? ???? (?????)</th>
</tr>
<tr>
<td>@Model.Address.EvacuationAddress</td>
</tr>
</table>
</li>
<li>
<label>6) ???????: ?????? ??? ?? ???? ??????? </label>
</li>
</ul>
</div>
<footer id="prt-footer">
<div id="prt-footer-img">
<img src="Files/Sign/@Model.Sign.Value" />
<img src="Files/Stamp/@Model.Stamp.Value" />
</div>
<div id="prt-footer-address">@Html.Raw(Model.FooterAddress.Value)</div>
</footer>
<div class="clear"></div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
但是输出pdf中有一些空框而不是图像.是MVC 5
问题HiQPdf
还是别的什么问题?
更新
我尝试了MVC 4
.Net 4和4.5,它不再工作了.我曾经以同样的方式使用它,没有任何错误,但现在它不起作用.
发现了一些文件,你可能会喜欢这里
转换器生成的PDF文档中缺少图像和样式,并且不执行脚本,因为这些元素由我转换的HTML代码中的相对URL引用.如何提供转换器用来解析这些元素的基本URL?
转换HtmlToPdf,HtmlToImage和HtmlToSvg类的HTML代码以及PdfHtml和PdfHtmlImage类的构造函数的方法有一个baseUrl参数,HiQPdf HTML转换器可以使用该参数来解析您转换的HTML代码中找到的相对URL.
例如,如果您转换的HTML代码中引用了图像,并且可以从http://www.example.com/Images/image.png完全限定的URL 访问该图像,则必须设置http:/ /www.example.com/作为应用程序代码中的基本URL.与您转换的HTML代码中引用的样式和脚本文件类似.
所以看起来你必须使用完全限定的名称来使它工作