I have a web page where QR code is displayed on a document as an image. The document is in .pdf format.
When I click on a button, it should download the document with the QR image placed on the document, But the code I have is not working.
I clicked on the download button and the document downloaded but the QR code image was not on it
C# code
public string GetUrl(string imagepath)
{
string[] splits = Request.Url.AbsoluteUri.Split('/');
if (splits.Length >= 2)
{
string url = splits[0] + "//";
for (int i = 2; i < splits.Length - 1; i++)
{
url += splits[i];
url += "/";
}
return url + imagepath;
}
return imagepath;
}
public void generatePDF()
{
string fileName = "Invoice" + DateTime.Now.ToString() + ".pdf";
Response.Clear();
Response.ContentType = "Application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
HtmlConverter.ConvertToPdf(getPanelHtml(), Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
public string getPanelHtml()
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
Panel1.RenderControl(hw);
var html = sb.ToString();
return html;
}
public string ToAbsoluteUrl(string relativeUrl)
{
if (string.IsNullOrEmpty(relativeUrl))
{
return relativeUrl;
}
if (relativeUrl.StartsWith("/"))
{
relativeUrl = relativeUrl.Insert(0, "~");
}
if (!relativeUrl.StartsWith("~/"))
{
relativeUrl = relativeUrl.Insert(0, "~/");
}
var url = HttpContext.Current.Request.Url;
var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
return String.Format("{0}://{1}{2}{3}", url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
}
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("MyConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Table WHERE Id_no = '" + Session["Record"] + "'";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = cmd;
sda.Fill(ds, "detail");
if (ds.Tables[0].Rows.Count > 0)
{
byte[] image = (byte[])ds.Tables[0].Rows[0]["logo"];
imgFileUpload.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);
string fileName = "Receipt" + DateTime.Now.ToString() + ".pdf";
byte[] QRBytes = GetQRCodeBytes(ToAbsoluteUrl("/ITdoc.aspx") + "?Id=" + Session["Record"]);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
var ImgUrl = Image1.ImageUrl;
var ImagUrl = imgFileUpload.ImageUrl;
// Write image as file to folder.
File.WriteAllBytes(ToAbsoluteUrl("qrimg.jpg"), QRBytes);
File.WriteAllBytes(ToAbsoluteUrl("logo.jpg"), image);
// convert and set absolute url in image.
Image1.ImageUrl = GetUrl("qrimg.jpg");
imgFileUpload.ImageUrl = GetUrl("logo.jpg");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
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=" + fileName + ";");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
// Delete the temp image.
File.Delete(ToAbsoluteUrl("qrimg.jpg"));
File.Delete(ToAbsoluteUrl("logo.jpg"));
Image1.ImageUrl = ImgUrl;
imgFileUpload.ImageUrl = ImagUrl;
Response.End();
}
}