2011-06-13 4 views
3

다음 코드를 사용하여 iTextSharp.text를 사용하여 pdf에 html을 작성합니다.Html PDF로 작성된 형식이 올바르지 않습니다.

하지만 HTML 형식으로 표시되는 pdf 파일은 내 HTML 형식이 아닙니다. 디자인이 제대로되지 않습니다.

pdf에 적절한 디자인을 표시하려면 어떻게해야합니까?

Document document = new Document(PageSize.A4_LANDSCAPE, 0, 0, 30, 65); 
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/") + "Temp/" + "parsetest1.pdf", FileMode.Create)); 
document.Open(); 
String htmlText = "<div style='margin: 20px auto; width: 300px; font-family: Arial, Helvetica, sans-serif;'><div style='background: rgb(216, 216, 216); padding: 7px;'><div style='background: rgb(255, 255, 255); padding: 10px; width: 57px; float: left;'><img alt='barcode' src='images/abc.png'></div><div style='width: 7px; height: 7px; float: left;'></div>   <div style='width: 166px; float: right;'><div style='background: rgb(255, 255, 255); padding: 10px;' align='center'><img alt='img' src='images/photpath.png'><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; margin-top: 7px;' align='center'><div style='color: rgb(157, 157, 157); font-size: 12px; float: left;'>Name</div><div style='width: 146px; text-align: center; color: rgb(0, 0, 0); clear: both; font-size: 14px; margin-top: 10px; float: left;' align='center'><strong>dalvirsaini</strong></div><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; margin-top: 7px;' align='center'><div style='color: rgb(157, 157, 157); font-size: 12px; float: left;'>Description</div><div style='width: 146px; text-align: center; color: rgb(0, 0, 0); clear: both; font-size: 14px; margin-top: 10px; float: left;' align='center'><strong>Payment Status</strong></div><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; height: 89px; margin-top: 7px;' align='center'><img alt='ScanCode' src='images/abc2.png'><div style='clear: both;'></div></div><div style='clear: both;'></div></div><div style='clear: both;'></div></div><div style='clear: both;'></div></div>"; 
StringReader abc = new StringReader(htmlText); 
List<iTextSharp.text.IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(abc, null); 
foreach (object item in elements) 
{ 
    document.Add((IElement)item); 
} 
document.Close(); 
+1

귀하의 HTML 유효 완전히 형성 HTML 아니다 - 유효한 HTML은있다 HTML 태그로 시작합니다. 스타터에게 간단하고 유효한 HTML을 사용해보십시오. –

+0

이 문제를 해결하는 방법을 찾았습니까? – Armance

답변

2

HTMLWorker는 더 이상 사용되지 않습니다. 그리고 좋은 이유가 있습니다. HTML 및 CSS 구문의 전체 집합을 지원하지 않으며 해당 기능을 업데이트하기 위해 적극적인 노력을 기울이고 있지 않습니다.

HTML5 및 CSS3을 지원하는 HTML을 PDF로 변환하기위한 최신 iText 솔루션 인 pdfHTML을 사용해 보시기 바랍니다.

작은 코드 샘플 :

// IO 
String html = "your_html_snippet"; 
File pdfDest = new File(System.getProperty("user.home"), "output.pdf"); 

// pdfHTML specific code 
ConverterProperties converterProperties = new ConverterProperties(); 
HtmlConverter.convertToPdf(html, new FileOutputStream(pdfDest), converterProperties); 

체크 아웃 자세한 내용은 iText를 웹 사이트 :

관련 문제