how to write file pdf when i convert html to pdf - asp.net

i try to use wkhtmltopdf.exe from server to convert html file to pdf then when process is done. application must to create file pdf . How can i do ? Please help.
this is code
string myDocumentsPath = "c:\\wkhtmltopdf.exe ";
ProcessStartInfo psi = new ProcessStartInfo(myDocumentsPath, " http://www.website.com/page");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
Process myProcess = Process.Start(psi);
myProcess.WaitForExit();
myProcess.Close();

Related

ClosedXML can't delete file after saving Workbook "process in use"

So here's the code:
string filename = #"c:\test.xlsx";
using (XLWorkbook wb = CreateWorkbookInformation())
{
wb.SaveAs(filename);
Email.EmailAsAttachment(filename);
}
File.Delete(filename);
It creates the Workbook information just fine, it saves the file fine, it emails the file fine as an attachment... However, when I try to delete file (after the using statement), it states the "process is in use". There shouldn't be anything keeping the file open?!? What process am I missing that I should close in order to delete the file?
Ahh, nevermind, had nothing to do with ClosedXML, had everything to do with attaching the file to the email and not using a proper usingstatement for the attachment. So the attachment process was keeping the file open.
Thank you so much for posting this answer!!!
I went crazy trying to understand what's my problem!
I did as you suggested and used the proper using statement:
using (MailMessage mail = new MailMessage(senderAdress, emailAdress))
{
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = ConfigurationManager.AppSettings["SMTPHostName"];
if (string.IsNullOrWhiteSpace(client.Host))
{
errorMsg = "ConfigurationManager.AppSettings[\"SMTPHostName\"] not found";
return false;
}
mail.Subject = subject;
mail.IsBodyHtml = isBodyHtml;
mail.Body = body;
if (!string.IsNullOrWhiteSpace(attachmentFullPath))
{
if (File.Exists(attachmentFullPath))
{
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
contentType.Name = Path.GetFileName(attachmentFullPath);
mail.Attachments.Add(new Attachment(attachmentFullPath, contentType));
}
else
{
errorMsg = "Attachment File Doesn't Exist: " + attachmentFullPath;
return false;
}
}
client.Send(mail);
}

How to use Windows task Scheduler in ASP.NET

I am trying to use ASP.NET in Window Task Scheduler. I want to send the email on specific time.
But ASP.NET is not run as an EXE, it has a dynamic ip address.
I have no idea to use Window Task Scheduler in ASP.NET.
Can you give me any solutions for it?
void SendEmail()
{
//get the data from database
DataTable data = GetData();
DataTable email_data = GetEmailData();
//set DataTable Name of Excel Sheet
data.TableName = "NSOList";
//Create a New Workook
using (XLWorkbook wb = new XLWorkbook())
{
//Add the DataTable as Excel Workhseet
wb.Worksheets.Add(data);
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Excel Workbook to MemoryStream
wb.SaveAs(memoryStream);
//Convert MemoryStream to Byte array
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
//body with embedded image
AlternateView body = AlternateView.CreateAlternateViewFromString
("Hi <br><br> <img src=cid:example>", null, "text/html");
//create the LinkedResource (embedded image)
LinkedResource image = new LinkedResource("c:\\example.png");
image.ContentId = "example";
//add the LinkedResource to the appropriate view
body.LinkedResources.Add(image);
String from = "abcd#abcd.net";
//bring Email data
for (int i = 0; i < email_data.Rows.Count; i++)
{
String to = email_data.Rows[i][0].ToString();
using (MailMessage mm = new MailMessage(from, to))
{
SmtpClient smtp = new SmtpClient();
mm.Subject = "List";
mm.AlternateViews.Add(body);
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "NSOList.xlsx"));
mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "abcd#gmail.com";
credentials.Password = "abcd";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
}
You should use a Task Scheduler like Quartz.Net. It allows you to define classes as Jobs, and then execute those jobs according to an schedule. I'm currently using it in some in-house projects and it performs as advertised.
EDITED
Check the answers here.
It should be a console application with your code in it. In bin folder it will create a .exe file which you need to use it in windows task scheduler.
Following link provides you a step by step procedure on how to create a task in windows task scheduler.
http://www.digitalcitizen.life/how-create-task-basic-task-wizard

how can i read output from console in asp.net

i am trying to read output from .exe file of a c++ program and trying to use this output for further use in my asp.net web application but it isn't working
this is what i have done so far
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
MyProcess.StartInfo.CreateNoWindow = true;
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.RedirectStandardOutput = true;
MyProcess.StartInfo.WorkingDirectory = "C:\\Users\\Hussain\\Documents";
MyProcess.StartInfo.FileName = "demo.exe";
MyProcess.StartInfo.Arguments = "MyArgument";
MyProcess.Start();
String s = MyProcess.StandardOutput.ReadToEnd();
System.IO.StreamWriter file = new
System.IO.StreamWriter("C:\\Users\\Hussain\\Documents\\123.txt");
file.WriteLine(s);
file.Close();
MyProcess.WaitForExit();
MyProcess.Close();

command prompt in asp.net and execute file in asp

I have a little asp.net program that in this program i could write and run commands of command prompt in my asp.net application An important part of this story is to run exe files with commands.
It is my code but dont run exe file for example where i write mspaint it dont open:
<pre lang="c#">
try
{
Process p = new Process();
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = c_path.Value = Encoding.Default.GetString(Convert.FromBase64String(c_path.Value));
c_text.Value = Encoding.Default.GetString(Convert.FromBase64String(c_text.Value));
p.StartInfo.Arguments = c_text.Value;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
string[] Uoc = new string[20];
for (int i = 1; Uoc.Length > i; i++)
{
Uoc[i] = p.StandardOutput.ReadLine();
Uoc[i] = Uoc[i].Replace("<", "<");
Uoc[i] = Uoc[i].Replace(">", ">");
Uoc[i] = Uoc[i].Replace("\r\n", "<br>");
}
string txt = "";
foreach (var item in Uoc)
{
txt += item + "<br />";
}
cmd_response.InnerHtml = "<hr width=\"100%\" noshade/><pre>" + txt + "";
cmd_response.Visible = true;
more.Visible = true;
}
catch (Exception error)
{
xseuB(error.Message);
}
</pre>
If you look at Task Manager and have it show processes for all users, you will see that the programs do run, but they are running under a different session. As a security precaution, the sessions are independent of each other. Programs in one session can not be interacted with by other sessions.

Calling console application from asp.net

I try to call console appliction from asp.net when i put breakpoint in console app why it can't stop there.How can i call console application?
var proc = Process.Start("D:\\Edefter\\EFaturaConsoleTxtParser\\bin\\Debug\\EFaturaConsoleTxtParser.exe", "/arg1 /arg2");
proc.WaitForExit();
I'm not sure about this but anyway you can try
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName =#"D:\\Edefter\\EFaturaConsoleTxtParser\\bin\\Debug\\EFaturaConsoleTxtParser.exe";
startinfo.CreateNoWindow = true;
startinfo.UseShellExecute = true;
Process myProcess = Process.Start(startinfo);
myProcess.Start();
While starting a process programmatically, the 'UserShellExcute' property must be 'false'. Otherwise, the CreateNoWindow property value gets ignored and new window is created.
Example:
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = #"demoApplication.exe";
startinfo.Arguments = "arg1 arg2";
startinfo.CreateNoWindow = true;
startinfo.UseShellExecute = false;
Process myProcess = Process.Start(startinfo);
Source: http://msdn.microsoft.com

Resources