Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Last active January 26, 2026 20:07
Show Gist options
  • Select an option

  • Save pedroinfo/48da366a78da2774e2e59525670fa7b9 to your computer and use it in GitHub Desktop.

Select an option

Save pedroinfo/48da366a78da2774e2e59525670fa7b9 to your computer and use it in GitHub Desktop.
PdfDownloadInterceptor
public static class PdfDownloadInterceptor
{
public static async Task HandleAsync(ResponseTransformContext context)
{
var proxyResponse = context.ProxyResponse;
var httpContext = context.HttpContext;
if (proxyResponse == null)
return;
if (proxyResponse.Content?.Headers.ContentType?.MediaType != "application/pdf")
return;
var originalPdf = await proxyResponse.Content.ReadAsByteArrayAsync();
var modifiedPdf = PdfProcessor.ChangePdf(originalPdf);
var response = httpContext.Response;
response.Headers.Clear();
// Force browser download
response.ContentType = MediaTypeNames.Application.Pdf;
response.Headers[HeaderNames.ContentDisposition] ="attachment; filename=\"document-processed.pdf\"";
response.ContentLength = modifiedPdf.Length;
await response.Body.WriteAsync(modifiedPdf);
context.SuppressResponseBody = true;
}
}
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Properties;
public static class PdfProcessor
{
public static byte[] ChangePdf(byte[] originalPdf)
{
using var input = new MemoryStream(originalPdf);
using var output = new MemoryStream();
using var reader = new PdfReader(input);
using var writer = new PdfWriter(output);
using var pdf = new PdfDocument(reader, writer);
var font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
int totalPages = pdf.GetNumberOfPages();
for (int i = 1; i <= totalPages; i++)
{
var page = pdf.GetPage(i);
Rectangle pageSize = page.GetPageSize();
var pdfCanvas = new PdfCanvas(
page.NewContentStreamAfter(),
page.GetResources(),
pdf
);
using var canvas = new Canvas(pdfCanvas, pageSize);
// Watermark
canvas
.SetFont(font)
.SetFontSize(48)
.SetFontColor(ColorConstants.LIGHT_GRAY)
.ShowTextAligned(
"GERADO VIA YARP",
pageSize.GetWidth() / 2,
pageSize.GetHeight() / 2,
TextAlignment.CENTER
);
// Rodapé
canvas
.SetFontSize(9)
.SetFontColor(ColorConstants.GRAY)
.ShowTextAligned(
$"Página {i} de {totalPages}",
pageSize.GetWidth() / 2,
20,
TextAlignment.CENTER
);
}
pdf.Close();
return output.ToArray();
}
}
using ProxyYarp.Services;
using Yarp.ReverseProxy.Transforms;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
.AddTransforms(builderContext =>
{
builderContext.AddResponseTransform(async context =>
{
await PdfDownloadInterceptor.HandleAsync(context);
});
});
var app = builder.Build();
app.Use(async (ctx, next) =>
{
Console.WriteLine($"Request: {ctx.Request.Method} {ctx.Request.Path}");
await next();
});
app.MapReverseProxy();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment