Reputation: 55
I'm working on asp.net application where it takes input from user in multiple form and save in pdf. My idea is , the user can shared the generated pdf via QR code. I have completed until saving the pdf in database. I'm stuck on how I can achieve this.
With this, I'm attaching code for saving pdf in database:-
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
MemoryStream memoryStream = new MemoryStream();
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
pdfDoc.Add(new Paragraph("My first PDF"));
htmlparser.Parse(sr);
pdfDoc.Close();
int id = 100;
int ic = 1234;
cmd = new SqlCommand("InsertPDF", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", id.ToString());
cmd.Parameters.AddWithValue("@Pdf", memoryStream.ToArray());
cmd.Parameters.AddWithValue("@IC", ic.ToString());
cmd.ExecuteNonQuery();
Upvotes: 0
Views: 464
Reputation: 218930
Is this to say that you're successfully saving it in the database, you just need to know how to share it?
Essentially, a QR code can simply be a URL. So you just need a unique URL for that PDF. This can be accomplished using the PDF's database identifier. (It looks like you're manually creating an identifier in the code, which is very odd, but as long as it's unique then we can glaze over that...)
If you're using WebForms, then you're looking to create something like this:
~/GetPDF.aspx?id=123
For MVC, something like this:
~/PDF/Share/123
(Or however you want to structure each URL, it's up to you. Admittedly I couldn't think of really meaningful names for the MVC one, but you know a lot more about your application and what it does so I imagine you should be able to come up with something suitable.)
So, basically, you need to create a page which accepts the unique ID of the PDF in the URL, uses that ID to fetch the PDF from the database, and then returns that PDF to the user.
For WebForms, returning the PDF to the user is a matter of clearing the response, writing the correct headers, writing the file data, and ending the response. Something like this:
Response.Clear();
Response.ContentType = "application/pdf";
Response.Write(pdfData);
Response.End();
In MVC it's a bit easier, you can return a file content result with the binary data you fetched from the database, perhaps something as simple as:
return File(pdfData, "application/pdf", "FileName.pdf");
Once you have this, then for any given PDF file you know what the unique URL is going to be. Just swap out the ID in the URL for the given file being shared. That string is all you need to create a QR code. In fact, Google has a handy API for this. In fact, you don't really have to do any coding at all for it.
Once you have the ID, you can just add an image to any page using the PDF URL with that ID. The URL for the image goes directly to Google and generates the QR code. Something like this:
http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=http://www.mysite.com/GetPDF.aspx?ID=123
For example, here's a QR code for this Stack Overflow question:
Examine the URL for that image (right-click and open it in a new tab, use FireBug, however you want to do it) to see the format of the URL. It's just the Google QR generator API followed by the text of the QR code, which in our case is a URL.
Upvotes: 2