Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have an input form which i have filled , now on the submission i want to send the details in the input form as a pdf in mail as an attachment


Code:


[Authorize]
[HttpGet]
public ActionResult InCountryForm(string ac)
{
DisplayLnd dn = new DisplayLnd();
DbAccess da = new DbAccess();
dn.Lnd_Program = new LndFeedback.Models.lnd_prgm();
dn.Lnd_Program.create_date = DateTime.Now;
dn.Lnd_Program.from_date = DateTime.Now;
dn.Lnd_Program.till_date = DateTime.Now;
dn.Lnd_Program.create_by = User.Identity.Name;
dn.Lnd_Program.prgm_id = ac;
dn.Lnd_Program.prgm_type = "IN COUNTRY";
dn.Lnd_Program.country_id = 72;
dn.Lnd_Program.no_of_attendees = "0";
dn.txt = DateTime.Now.ToString("dddd, dd MMMM yyyy");
dn.nwTm = DateTime.Now.ToShortTimeString();
//dn.Lnd_Program.from_date = dn.Lnd_Program.from_date;
//dn.Lnd_Program.till_date = dn.Lnd_Program.till_date;
dn.ListAgency = da.FindAgencyList();
dn.ListState = da.FindStates();
return View(dn);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult InCountryForm(DisplayLnd dn)
{

DbAccess da = new DbAccess();
lnd_prgm Lnd_Program = dn.Lnd_Program;

The DisplayLnd object has all the details filled in the GET Method
i want to create the pdf in the post method and then mail it toa person.

What I have tried:

the pdf created should not be stored in any server. So basically on th fly pdf and then mail it as an attachment.
Posted
Updated 25-Apr-19 4:15am
Comments
Member 14330973 26-Apr-19 0:55am    
I have used the following code in post method
string appl_str = "Dear Sir/Madam,We have received feedback with the following details against your application ID " + "" +dn.Lnd_Program.prgm_id + ":";
appl_str = appl_str + "" + " Applicant Details";
appl_str = appl_str + "";
appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "Name " + fname + " Salary Code " + lnd_app.create_by + " Gender" + dis.gender.ToUpper() + " Father's Name" + award_app.APPL_FATHER + " Mother's Name" + award_app.APPL_MOTHER + " Category" + dis.category.ToUpper() + " Class" + dis.cat_class.ToUpper() + " Institute" + award_app.APPL_SCHOOL_COLLEGE + " Preferred Language" + award_app.APPL_PREFERRED_LANGUAGE + " ";
//appl_str = appl_str + "" + " Applicant Correspondence Address";
//appl_str = appl_str + "";
//appl_str = appl_str + "table, th, td {border: 1px solid black;}th, td {padding: 8px;text-align: center;} Applicant Correspondence Address";
appl_str = appl_str + "";
appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
//appl_str = appl_str + "";
appl_str = appl_str + "Address" + award_app.APPL_ADDR_1 + " " + award_app.APPL_ADDR_2 + " Town/City" + dn.Lnd_Program.agency_name + " District" + dn.Lnd_Program.pgm_city + " State" + dis.add_state + " Pin/City" + award_app.APPL_ADDR_PIN + " Mobile Number/City" + award_app.APPL_MOBILE_NO + " Alternate Mobile Number" + award_app.APPL_ALT_MOBILE_NO + " Email" + award_app.APPL_ADDR_EMAIL + " Submission Date and Time" + dn.Lnd_Program.create_date + " ";
appl_str = appl_str + "For any query please mail us at test@test.in with your application ID.";
appl_str = appl_str + "Regards" + "XYZ" + "";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + dn.Lnd_Program.prgm_id.ToString() + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(GetPDF(appl_str));
Response.End();
But this downloads the pdf file......how to use the above pdf as an mail attachment.

GetPDF Function.....

public byte[] GetPDF(string pHTML)
{
byte[] bPDF = null;

MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);

// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

// 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

// 3: we create a worker parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);

// 4: we open document and start the worker on the document
doc.Open();
htmlWorker.StartDocument();


// 5: parse the html into the document
htmlWorker.Parse(txtReader);

// 6: close the document and the worker
htmlWorker.EndDocument();
htmlWorker.Close();
doc.Close();

bPDF = ms.ToArray();

return bPDF;

1 solution

Probably the best way to do this would be to have a Template PDF, and using the Acrobat SDK fill in the blanks for that template. Once you have that completed, you could take the PDF object from memory and encode it directly into the email body.

I personally would save the PDF at least temporarily and then use the attachment methods to put the PDF into the email.
 
Share this answer
 
Comments
Member 14330973 25-Apr-19 22:10pm    
how can i save the pdf temporarily and then use it in the mail. since after filling up the input form, the submit button should automatically trigger the mail along with the attachment.
MadMyche 26-Apr-19 6:46am    
The server-side scrip that sends the email should do the PDF creation prior to sending the mail

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900