Hello Forum,
I recently learnt how to convert HTML to PDF with HTML style CSS. However, I encountered TWO ISSUES.
- How can I make an image that will be uploaded onto the image control to be rendered in the PDF? Because in the code, it has a complete image path which means that the image is inside the image folder of the project. But in this case image will be uploaded on to the image control
- Also, in my invoice the label controls appear well, as expected in the PDF but the gridview width did not cover the PDF width. How can I make the gridview width to cover the width of the PDF? I want to learn how to solve this two issues please.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;
using System.Text;
using iText.Html2pdf;
public partial class AA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item"), new DataColumn("Qty"), new DataColumn("Rate"), new DataColumn("Amount") });
dt.Rows.Add("Supply Of Electrical Equipments", "20", "3000", "60000");
dt.Rows.Add("Construction of Bore Hole", "2", "350000", "700000");
dt.Rows.Add("Construction of Fish Pond", "7", "45000", "315000");
dt.Rows.Add("Purcahse of Water Pump", "2", "70000", "140000");
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document();
PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
From the code below and in private void BindCardAndQR. In the code, Image4 is the image control to bind QR code
public byte[] GetQRCodeBytes(string url)
{
QRCodeEncoder encoder = new QRCodeEncoder();
Bitmap bi = encoder.Encode(url);
MemoryStream tmpSteam = new MemoryStream();
bi.Save(tmpSteam, ImageFormat.Jpeg);
return tmpSteam.ToArray();
}
private void BindCardAndQR()
{
// Bind Image4 for QR code
byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/IDCard.aspx") + "?Id=" + id);
Image4.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
}