I'd like to apologize, my English is not very good. I hope that you help me. I have this string from xslt trans.:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<html>\r\n <head />\r\n <body style=\"font-family: Verdana, sans-serif;\">\r\n ĚŠČŘŽŘÝŽÝÁÁÍÉŮ ěščřžýáíéůú\r\n </body>\r\n</html>"
and when I trying convert this html string to pdf:
private static byte[] CreatePdfUsingXslt(string htmlText)
{
var msOutput = new MemoryStream();
var reader = new StringReader(htmlText);
var document = new Document(PageSize.A4, 30, 30, 30, 30);
var pdfWriter = PdfWriter.GetInstance(document, msOutput);
var worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
return msOutput.GetBuffer();
}
protected void SavePDF(byte[] storeData, string fileName)
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.ContentType = "application/pdf";
Response.Charset = Encoding.UTF8.ToString();
Response.ContentEncoding = Encoding.UTF8;
Response.Buffer = true;
Response.BinaryWrite(storeData);
Response.Flush();
Response.End();
}
I'm getting something like this: ŠŽÝŽÝÁÁÍÉ šžýáíéú and it's wrong... My result doesn't have characters like Ě,Č,Ř,Ů,... These are czech language characters.
I try to use XMLParser like this:
private static byte[] CreatePdfUsingXslt(string htmlText, string serverPath)
{
var msInput = new MemoryStream(Encoding.UTF8.GetBytes(htmlText));
var msOutput = new MemoryStream();
msInput.Position = 0;
msOutput.Position = 0;
var doc = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msOutput);
var htmlPipelineContext = new HtmlPipelineContext();
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
var cssPath = String.Format("{0}XsltTemplates\\print.css", serverPath);
cssResolver.AddCssFile(cssPath,true);
var pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlPipelineContext, new PdfWriterPipeline(doc, pdfWriter)));
var xmlWorker = new XMLWorker(pipeline, true);
var xmlParser = new XMLParser(true, xmlWorker);
xmlParser.Parse(new StreamReader(msInput, Encoding.UTF8));
xmlParser.Flush();
doc.Close();
return msOutput.ToArray();
}
but function xmlParser.Parse(new StreamReader(msInput, Encoding.UTF8)); throw NullReferenceException.
This is more important for me. Do you have anyone any idea, how I fix it?
Related
This my code, why do it produces the error?
sMailBody=MailstrBody.ToString();
StringBuilder sample = new StringBuilder();
sample = MailstrBody;
string passno = dsMail1.Tables[0].Rows[0]["PASSNO"].ToString();
HttpResponse Response = HttpContext.Current.Response;
EmailContentDAL example = new EmailContentDAL();
example.Pdf(sample, Response, passno);
public void Pdf(StringBuilder sample, HttpResponse currentResponse, string passno)
{
StringReader sr = new StringReader(sample.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc,
currentResponse.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
currentResponse.Clear();
currentResponse.ContentType = "application/pdf";
currentResponse.AddHeader("Content-Disposition", "attachment; filename=GetPass_" + passno + ".pdf");
currentResponse.Buffer = true;
currentResponse.Cache.SetCacheability(HttpCacheability.NoCache);
currentResponse.Write(pdfDoc);
currentResponse.End();
}
The above code produces null reference error at htmlparser.Parse(sr), but at StringReader sr = new StringReader(sample.ToString()), sample.toString() shows a value.
I am working on Export to PDF funtionality using C# and PDFSharp. I am getting this error:
Value cannot be null.
Parameter name: elementId
The error is on this line :
PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);
Here's the whole method:
public bool ExportPdf(string htmlcontenttbl)
{
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=Myfile.pdf");
//Response.AddHeader("Content-Disposition", "inline;filename=file.pdf");
//Response.AppendHeader("Content-Disposition", "attachment; filename=Myfile.pdf");
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);
var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.PageSize = PageSize.A4;
config.MarginBottom = 30;
config.MarginTop = 30;
//PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl, config);
byte[] bytes = null;
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, true);
bytes = stream.ToArray();
}
//var path1 = Server.MapPath("~/Images/" + DateTime.Now.TimeOfDay.Ticks + "result.pdf");
//System.IO.File.WriteAllBytes(path1, bytes);
//Response.TransmitFile(path1, 0, bytes.Length);
//Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return true;
}
That exception can occur if you have a blank anchor tag.
It is likely ultimately being generated from here.
You should remove the blank anchor tag.
I am trying to export a div to pdf with style using code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Fullname.Text + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var cssText = System.IO.File.ReadAllText(Server.MapPath("/css/bootstrap.css"));
var memoryStream = new MemoryStream();
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
pfd.RenderControl(htmlTextWriter);
StringReader stringReader = new StringReader(stringWriter.ToString().Replace("<br>", "<br/>").Replace("<td>", "<td/>").Replace("<tr>", "<tr/>").Replace("<td>", "<td/>"));
Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, Doc, stringReader);
Doc.Close();
Response.Write(Doc);
Response.End();
How i can attach css file to pdf to keep same appearance in HTML?
Try below code,
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
byte[] pdf; // result will be here
var cssText = File.ReadAllText(MapPath("~/css/style.css"));
var html = File.ReadAllText(MapPath("~/css/index.html"));
using (var memoryStream = new MemoryStream())
{
var document = new Document(PageSize.A4, 20, 20, 30, 30);
var writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
}
}
document.Close();
pdf = memoryStream.ToArray();
}
I'm trying to generate a pdf file using itextsharp.
Here is the method that's supposed to generate the PDF:
private void Page_onPreRenderComplete(object sender, EventArgs e)
{
// createPdf.GeneratePDF(htmlMarkup);
MemoryStream memoryStream = new MemoryStream();
StringBuilder sBuilder = new StringBuilder();
StringWriter sw = new StringWriter(sBuilder);
HtmlTextWriter htmlText = new HtmlTextWriter(sw);
Page.RenderControl(htmlText);
string pdfBody = sBuilder.ToString();
Document document = new Document();
PdfWriter.GetInstance(document, memoryStream);
document.Open();
StyleSheet styles = new StyleSheet();
HTMLWorker hw = new HTMLWorker(document);
try
{
hw.Parse(new StringReader(pdfBody));
}
catch (Exception ex)
{
string msg = ex.Message;
}
finally
{
document.Close();
}
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.Write(memoryStream);
HttpContext.Current.Response.End();
}
an error is generated on the line within the try block. How can I fix this?
may be the image tags etc are in relative paths instead of absolute paths in the rendered HTML
var htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), styles);
document.Open();
BaseFont Vn_Helvetica = BaseFont.CreateFont(#"C:\Windows\Fonts\arial.ttf",
"Identity-H", BaseFont.EMBEDDED);
Font fontNormal = new Font(Vn_Helvetica, 12, Font.NORMAL);
foreach (var t in htmlarraylist)
{
if (t is PdfPTable)
{
//how set fontNormal all text from Pdfptable ?
}
document.Add((IElement)t);
}
document.Close();
Can someone help me please
Please try by setting font to the PdfPTable.DefaultCell property in you foreach loop
Example:
t.DefaultCell.Phrase = new Phrase() { Font = fontNormal };
i have try set font family in pdf template. and show the following error also
'BaseFont' does not contain a definition for 'Cambria'
please suggest me...thanks
public void abcd()
{
try
{
string UniqueNumber = Request.QueryString["UniqueNumber"];
string strFileName = UniqueNumber;
string strFileExtension = ".pdf";
string strContentType = FileManager.FileContentType_application_pdf;
string strExportData = string.Empty;
Document pdfDoc = new Document();
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
var output = new MemoryStream();
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.Cambria, BaseFont.CP1252, false);
//Font ChronicleFont = new Font(bfTimes, 26f);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
StringReader sr;
sr = new StringReader(Convert.ToString(ExportData.UserDetails(UniqueNumber)));
PdfWriter.GetInstance(pdfDoc, output);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
strFileName = strFileName.Replace(" - ", "-").Replace(" ", "-").Replace("--", "-");
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}{1}", strFileName, strFileExtension));
Response.ContentType = strContentType;
Response.Charset = "";
Response.BinaryWrite(output.ToArray());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
//
}
}