Created
February 6, 2020 14:42
-
-
Save aspose-com-gists/cff53cfa900b8d4959be12e963e2598e to your computer and use it in GitHub Desktop.
PDF Creator - ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
public FileResult Index(string editor1) | |
{ | |
// create a unique file name | |
string fileName = Guid.NewGuid() + ".pdf"; | |
// convert HTML text to stream | |
byte[] byteArray = Encoding.UTF8.GetBytes(editor1); | |
// generate PDF from the HTML | |
MemoryStream stream = new MemoryStream(byteArray); | |
HtmlLoadOptions options = new HtmlLoadOptions(); | |
Document pdfDocument = new Document(stream, options); | |
// create memory stream for the PDF file | |
Stream outputStream = new MemoryStream(); | |
pdfDocument.Save(outputStream); | |
// return generated PDF file | |
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName); | |
} | |
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | |
public IActionResult Error() | |
{ | |
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewData["Title"] = "PDF Creator"; | |
} | |
<script src="~/ckeditor/ckeditor.js"></script> | |
<div class="row"> | |
<div class="col-md-12"> | |
<h2>Create a PDF File</h2> | |
<form method="post"> | |
<textarea name="editor1" id="editor1" rows="80" cols="80"> | |
Start creating your PDF document. | |
</textarea> | |
<br /> | |
<input type="submit" class="btn btn-success" value="Generate PDF" /> | |
<script> | |
// Replace the <textarea id="editor1"> with a CKEditor | |
// instance, using default configuration. | |
CKEDITOR.replace('editor1'); | |
</script> | |
</form> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment