HTML page Download as PDF using WEB API
Every time Generate Pdf name different
like
1st time --- example.pdf
2nd time - example2.pdf
Know the answer? Post it. Somebody with the same question will find it here.
You can take the HTML page and convert it to PDF through a backend service using the DocumentConverter. Here is an example you can leverage:
using (DocumentConverter documentConverter = new DocumentConverter())
{
var input = @"C:\Inputfiles\page.html");
var output = @"C:\Outputfiles\output.pdf");
var jobData = DocumentConverterJobs.CreateJobData(input, output, DocumentFormat.Pdf);
jobData.JobName = "Convert to Pdf";
var documentWriter = new DocumentWriter();
documentConverter.SetDocumentWriterInstance(documentWriter);
var renderingEngine = new AnnWinFormsRenderingEngine();
documentConverter.SetAnnRenderingEngineInstance(renderingEngine);
var job = documentConverter.Jobs.CreateJob(jobData);
documentConverter.Jobs.RunJob(job);
}
https://www.leadtools.com/help/sdk/tutorials/dotnet-core-convert-files-with-the-document-converter.html
https://www.leadtools.com/help/sdk/tutorials/dotnet-core-configure-and-run-the-document-service.html

wkhtmltopdf.public static string HtmlToPdf(string outputFilenamePrefix, string[] urls,
string[] options = null,
string pdfHtmlToPdfExePath = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")
{
string urlsSeparatedBySpaces = string.Empty;
try
{
//Determine inputs
if ((urls == null) || (urls.Length == 0))
throw new Exception("No input URLs provided for HtmlToPdf");
else
urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs
string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name
var p = new System.Diagnostics.Process()
{
StartInfo =
{
FileName = pdfHtmlToPdfExePath,
Arguments = ((options == null) ? "" : string.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,
UseShellExecute = false, // needs to be false in order to redirect output
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
WorkingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
}
};
p.Start();
// read the output here...
var output = p.StandardOutput.ReadToEnd();
var errorOutput = p.StandardError.ReadToEnd();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
// if 0 or 2, it worked so return path of pdf
if ((returnCode == 0) || (returnCode == 2))
return outputFilename;
else
throw new Exception(errorOutput);
}
catch (Exception exc)
{
throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
}
}HtmlToPdf("test", new string[] { "https://www.google.com" }, new string[] { "-s A5" });HTML string to PDF, the tweak the above method and replace the Arguments to Process StartInfo as $@"/C echo | set /p=""{htmlText}"" | ""{pdfHtmlToPdfExePath}"" {((options == null) ? "" : string.Join(" ", options))} - ""C:\Users\xxxx\Desktop\{outputFilename}""";Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sonu GuptaRank #1,261 · 1.4K reputationPosted Aug 27, 2019, 12:18 AM EDT
Sapna GirishRank #1,810 · 819 reputationPosted Sep 5, 2019, 4:23 PM EDT
Jose SaizRank #5,346 · 302 reputationPosted Sep 6, 2019, 8:35 AM EDT