Skip to content

Commit

Permalink
Update PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed May 23, 2024
1 parent 5cbf606 commit 28cf82b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
8 changes: 3 additions & 5 deletions ApiCreatePdf/Controllers/DownloadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ namespace ApiCreatePdf.Controllers
{
[ApiController]
[Route("[controller]")]
public class DownloadController : ControllerBase
public class DownloadController(DocumentService _documentService) : ControllerBase
{

[Route("pdf/{id}")]
[HttpGet]
public FileStreamResult Download(int id)
public FileStreamResult Download(string id)
{
var path = "test\\test.pdf";
var stream = new FileStream(path, FileMode.Open);
var stream = _documentService.GeneratePdf(id);
return File(stream, "application/pdf");
}
}
Expand Down
7 changes: 7 additions & 0 deletions ApiCreatePdf/DocumentData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ApiCreatePdf
{
public class DocumentData
{
public string MainContentText { get; set; } = string.Empty;
}
}
39 changes: 39 additions & 0 deletions ApiCreatePdf/DocumentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using GemBox.Document;

namespace ApiCreatePdf;

public class DocumentService
{
public Stream GeneratePdf(string id)
{
var documentData = GetDocumentData(id);

var pdf = new MemoryStream();

// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

var document = new DocumentModel();

var section = new Section(document);
document.Sections.Add(section);

var paragraph = new Paragraph(document);
section.Blocks.Add(paragraph);

var run = new Run(document, documentData.MainContentText);
paragraph.Inlines.Add(run);

document.Save(pdf, SaveOptions.PdfDefault);

return pdf;
}

private DocumentData GetDocumentData(string id)
{
return new DocumentData
{
MainContentText = $"PDF created for id: {id}"
};
}
}
7 changes: 3 additions & 4 deletions ApiCreatePdf/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using ApiCreatePdf;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddScoped<DocumentService>();

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Expand All @@ -17,9 +18,7 @@
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

0 comments on commit 28cf82b

Please sign in to comment.