Hi
Can you upload a message error if exist or what show on the display when de app doesnt work?
Can somebody please tell me why a code can work on a local server but cannot work on a host server?
Why is it that I have a code that works on a local server but when hosted on an online host server, it does not seem to work?
Another fascinating thing is that, upon uploading it to an online host server , it worked the first time, then after about 5 hours, it stopped working.
I have this code that generates pdf from webpage with texts and images and its quite working on my local machine but not working on my website hosted online.
Here is the code
Namespace
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Data;
using System.Configuration;
using MessagingToolkit.QRCode.Codec.Data;
using MessagingToolkit.QRCode.Codec;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Data.SqlClient;
using iText.Html2pdf;
using System.Security.Policy;
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;
Code - this generates a 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();
}
the PDF.
it actually saves the image in the root directory but rendering it in the pdf is the issue
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM DocTable WHERE reference_no = '" + lblprefix.Text + "'";
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);
byte[] QRBytes = GetQRCodeBytes(ToAbsoluteUrl("/temp.aspx"));
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
// this line saves/writes the images as file to folder.
File.WriteAllBytes(Server.MapPath("img.jpg"), QRBytes);
File.WriteAllBytes(Server.MapPath("logo.jpg"), image);
//Then it converts and set absolute url in the images.
Image1.ImageUrl = GetUrl("img.jpg");
imgFileUpload.ImageUrl = GetUrl("logo.jpg");
}
//This keeps images intact
var ImgUrl = Image1.ImageUrl;
var ImagUrl = imgFileUpload.ImageUrl;
string fileName = "Document" + DateTime.Now.ToString() + ".pdf";
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);
//After pdf has been done, delete the images that were saved in folder.
File.Delete(Server.MapPath("img.jpg"));
File.Delete(Server.MapPath("logo.jpg"));
Image1.ImageUrl = ImgUrl;
imgFileUpload.ImageUrl = ImagUrl;
Response.End();
}
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;
}
Developer technologies | ASP.NET | Other
Developer technologies | C#
1 answer
Sort by: Most helpful
-
Lemusjimmy 1 Reputation point
2022-12-10T14:25:57.277+00:00