我想知道itextsharp是否只接受<table>
。问题是使用<div>
s?
下面的外部css文件是从cshtml中调用的。相同的代码可以很好地创建html,失败的pdf。我正在使用XMLWorkerHelper
。
RenderViewToString方法:
public class HtmlViewRenderer
{
public string RenderViewToString(Controller controller, string viewName, object viewData)
{
var renderedView = new StringBuilder();
using (var responseWriter = new StringWriter(renderedView))
{
var fakeResponse = new HttpResponse(responseWriter);
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), controller.ControllerContext.RouteData, controller.ControllerContext.Controller);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
using (var viewPage = new ViewPage())
{
var html = new HtmlHelper(CreateViewContext(responseWriter, fakeControllerContext), viewPage);
html.RenderPartial(viewName, viewData);
HttpContext.Current = oldContext;
}
}
return renderedView.ToString();
}
private static ViewContext CreateViewContext(TextWriter responseWriter, ControllerContext fakeControllerContext)
{
return new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), responseWriter);
}
}
StandardPdfRenderClass - 渲染方法:
public class StandardPdfRenderer
{
private const int HorizontalMargin = 10;
private const int VerticalMargin = 10;
public byte[] Render(string htmlText, string pageTitle)
{
byte[] renderedBuffer;
//BaseFont Vn_Helvetica = BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", "Identity-H", iTextSharp.text.pdf.BaseFont.EMBEDDED);
BaseFont Vn_Helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
Font fontNormal = new Font(Vn_Helvetica, 12, iTextSharp.text.Font.NORMAL);
using (var outputMemoryStream = new MemoryStream())
{
using (var pdfDocument = new Document(PageSize.A3, HorizontalMargin, HorizontalMargin, 110, 30))
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
pdfWriter.PageEvent = new PrintHeaderFooter();
using (var htmlViewReader = new StringReader(TurkceKarakter(htmlText)))
{
pdfDocument.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, pdfDocument, htmlViewReader);
//used to be like this. Changed it to include XMLWorkerHelper up there.
//using (var htmlWorker = new HTMLWorker(pdfDocument))
//{
// htmlWorker.Parse(htmlViewReader);
//}
}
}
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
return renderedBuffer;
}
public string TurkceKarakter(string text)
{
text = text.Replace("İ", "I");
text = text.Replace("ı", "i");
text = text.Replace("Ş", "S");
text = text.Replace("ş", "s");
text = text.Replace("Ğ", "G");
text = text.Replace("ğ", "g");
text = text.Replace("Ö", "Ö");
text = text.Replace("ö", "ö");
text = text.Replace("ç", "c");
text = text.Replace("Ç", "C");
text = text.Replace("ü", "ü");
text = text.Replace("Ü", "Ü");
return text;
}
}
这是cshtml:
<div id="maincontainer">
<div id="maincontent">
<div class="section group">
<div class="col span_1_of_1">
<div class="baslik"><span class="mavi">.</span><h3>CV</h3></div>
</div>
</div>
<div class="section group">
<div class="col span_1_of_2">
<div>
<img src="@(helper.StaticUrl + helper.Detay.Detay.GenelBilgi.Resim)" width="180" alt="Resim" />
</div>
<br /><br /><br /><br />
<div class="col span_1_of_1 clearfix">
<div class="isim">
<span class="adisoyadi">@(helper.Detay.Detay.GenelBilgi.AdiSoyadi.ToUpper())</span>
</div>
</div>
</div>
<div class="col span_1_of_2 clearfix">
<dl class="iletisim">
<dt class="uk-icon-mobile">Telefon</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.CepTelefonUlkeKod) @(helper.Detay.Detay.GenelBilgi.CepTelefon) </dd>
<dt class=" uk-icon-email">E-mail</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.Email)</dd>
<dt class="uk-icon-adres">Adres</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.Adres) </dd>
<dt class="uk-icon-adres">Doğum Tarihi</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.DogumTarihi)</dd>
<dt class="uk-icon-adres">Medeni Durum</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.MedeniDurum)</dd>
@if (!string.IsNullOrEmpty(helper.Detay.Detay.GenelBilgi.AskerlikDurum))
{
<dt class=" uk-icon-adres">Askerlik</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.AskerlikDurum)</dd>
}
<dt class="uk-icon-adres">Çalışma Durumu</dt>
<dd>@(helper.Detay.Detay.GenelBilgi.CalismaDurum.HasValue ? helper.Detay.Detay.GenelBilgi.CalismaDurum.Value ? "Çalışıyor" : "Çalışmıyor" : "")</dd>
</dl>
</div>
<div class="col span_1_of_1 aciklama">
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
你可以将它用于非常基本的html到pdf,但即使是他们的网站也说xml worker真的是一个模板系统。 itext functionality comparison。它实际上不支持高级布局等...我最近在一个项目中遇到了这个问题,我参与了这个项目以及为什么我开始研究下面的工具。
如果您能够使用其他工具,则可以尝试使用像TuesPechkin这样的库,它是wkhtmltopdf的.net包装器。 Wkhtmltopdf使用QT Webkit渲染引擎,并对html5 / css3提供强大支持。