Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions HtmlPdfPlus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{501D5A85-0
ProjectSection(SolutionItems) = preProject
README.md = README.md
src\README.txt = src\README.txt
..\docs\swimlanes.io.AnyProcess.txt = ..\docs\swimlanes.io.AnyProcess.txt
..\docs\swimlanes.io.http.txt = ..\docs\swimlanes.io.http.txt
..\docs\swimlanes.io.OnlyServer.txt = ..\docs\swimlanes.io.OnlyServer.txt
docs\swimlanes.io.AnyProcess.txt = docs\swimlanes.io.AnyProcess.txt
docs\swimlanes.io.Http.txt = docs\swimlanes.io.Http.txt
docs\swimlanes.io.OnlyServer.txt = docs\swimlanes.io.OnlyServer.txt
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "XmlDocMarkdown", "XmlDocMarkdown", "{A9894106-2EBA-4867-AE60-869D39CC2294}"
Expand Down
106 changes: 80 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,23 @@ The current version (V.1.50.0) of **Playwright** supports **only the Chromium br
- AfterPDF : Save file, Send to cloud, etc
- Disable features to improve/ balance performance (minify, compress and log)

#### What's new in the latest version

- v0.3.0-beta (latest version)
### What's new in the latest version

- **v0.4.0-beta (latest version)**
- Relaxation of Package Reference for .net8 to .net9
- Renamed the 'Source' command to 'Scope'
- Renamed the 'Request' command to 'ScopeRequest'
- Changed parameter in funcion SubmitHtmlToPdf to byte[] instead of string
- Changed parameter for command Run to byte[] instead of string
- Changed parameter for command ScopeRequest to byte[] instead of string
- Removed DecompressBytes() method to class HtmlPdfResult
- Added DecompressOutputData() method to class HtmlPdfResult for custom scenarios
- Improvements in the compression/decompression process to use asynchronous methods
- Small code reviews
- Updated documentation
- Preparation for GA version

- **v0.3.0-beta**
- Added FromUrl(Uri value) command to client-side mode
- Fixed bug in server mode for multi thread safe when there is parameter customization and/or no client mode sending.
- Moved the BeforePDF(Func<string, TIn?, CancellationToken, Task<string>> inputParam) command to the execution context.
Expand All @@ -73,7 +87,8 @@ The current version (V.1.50.0) of **Playwright** supports **only the Chromium br
- Added command FromHtml(string html, int converttimeout = 30000, bool minify = true)
- Added command FromUrl(Uri value, int converttimeout = 30000)
- Added command FromRazor\<T\>(string template, T model, int converttimeout = 30000, bool minify = true)
- v0.2.0-beta

- **v0.2.0-beta**
- Initial version

## Prerequisites
Expand All @@ -95,9 +110,9 @@ dotnet tool install --global Microsoft.Playwright.CLI
playwright.exe install --with-deps
```

_Note: Make sure that the path to the executable is mapped to: C:\Users\[YourU)ser]\.dotnet\tools._
_Note: Make sure that the path to the executable is mapped to: C:\Users\\[login]\\.dotnet\tools._

_If it is not, run it directly via the path C:\Users\[YourUser]\.dotnet\tools\playwright.exe install --with-deps__
_If it is not, run it directly via the path C:\Users\\[login]\\.dotnet\tools\playwright.exe install --with-deps_

### Installation Steps for HtmlPdfPlus

Expand Down Expand Up @@ -164,11 +179,20 @@ var clienthttp = HostApp!.Services
.CreateClient("HtmlPdfServer");

//create client instance and send to HtmlPdfPlus server endpoint
var pdfresult = await HtmlPdfClient.Create("HtmlPdfPlusClient")
.PageConfig((cfg) => cfg.Margins(10))
.FromHtml(HtmlSample())
.Timeout(5000)
.Run(clienthttp, token);
var pdfresult = await HtmlPdfClient
.Create("HtmlPdfPlusClient")
.PageConfig((cfg) =>
{
cfg.Margins(10)
.Footer("'<span style=\"text-align: center;width: 100%;font-size: 10px\"> <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></span>")
.Header("'<span style=\"text-align: center;width: 100%;font-size: 10px\" class=\"title\"></span>")
.Orientation(PageOrientation.Landscape)
.DisplayHeaderFooter(true);
})
.Logger(HostApp.Services.GetService<ILogger<Program>>())
.FromHtml(HtmlSample())
.Timeout(5000)
.Run(clienthttp, applifetime.ApplicationStopping);

//performs writing to file after performing conversion
if (pdfresult.IsSuccess)
Expand All @@ -194,9 +218,11 @@ builder.Services.AddHtmlPdfService((cfg) =>
});
...

app.MapPost("/GeneratePdf", async ([FromServices] IHtmlPdfServer<object, byte[]> PDFserver, [FromBody] string requestclienthtmltopdf, CancellationToken token) =>
app.MapPost("/GeneratePdf", async ([FromServices] IHtmlPdfServer<object, byte[]> PDFserver, [FromBody] Stream requestclienthtmltopdf, CancellationToken token) =>
{
return await PDFserver.Run(requestclienthtmltopdf, token);
var data = await requestclienthtmltopdf.ReadToBytesAsync();
return await PDFserver
.Run(data, token);
}).Produces<HtmlPdfResult<byte[]>>(200);

```
Expand All @@ -211,24 +237,31 @@ app.MapPost("/GeneratePdf", async ([FromServices] IHtmlPdfServer<object, byte[]>
```csharp
using HtmlPdfPlus;

//create client instance and send to HtmlPdfPlus server endpoint
var pdfresult = await HtmlPdfClient.Create("HtmlPdfPlusClient")
.PageConfig((cfg) => cfg.Margins(10))
.FromHtml(HtmlSample())
.Timeout(5000)
.Run(SendToServer, token);
// Generic suggestion for writing a file to a cloud like gcp/azure
// Suggested return would be the full path "repo/filename"
var paramTosave = new DataSavePDF("Filename.pdf","MyRepo","MyConnectionstring");

//performs writing to file after performing conversion
var pdfresult = await HtmlPdfClient.Create("HtmlPdfPlusClient")
.PageConfig((cfg) =>
{
cfg.Margins(10);
})
.Logger(HostApp.Services.GetService<ILogger<Program>>())
.FromRazor(TemplateRazor(), order1)
.Timeout(50000)
.Run<DataSavePDF,string>(SendToServer,paramTosave, applifetime.ApplicationStopping);

//Shwo result
if (pdfresult.IsSuccess)
{
await File.WriteAllBytesAsync("html2pdfsample.pdf", pdfresult.OutputData!);
Console.WriteLine($"File PDF generate at {pdfresult.OutputData}");
}
else
{
//show error via pdfresult.Error
Console.WriteLine($"HtmlPdfClient error: {pdfresult.Error!}");
}

private static async Task<HtmlPdfResult<byte[]>> SendToServer(string requestdata, CancellationToken token)
private static async Task<HtmlPdfResult<string>> SendToServer(byte[] requestdata, CancellationToken token)
{
//send requestdata to server and return result
}
Expand All @@ -242,14 +275,35 @@ using HtmlPdfPlus;

...
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHtmlPdfService((cfg) =>
builder.Services.AddHtmlPdfService<DataSavePDF,string>((cfg) =>
{
cfg.Logger(LogLevel.Debug, "MyPDFServer");
});
...
var PDFserver = HostApp.Services.GetHtmlPdfService();

var result = await PDFserver.Run(requestdata , Token);
var result = await PDFserver
.ScopeRequest(data)
.BeforePDF( (html,inputparam, _) =>
{
if (inputparam is null)
{
return Task.FromResult(html);
}
//performs replacement token substitution in the HTML source before performing the conversion
var aux = html.Replace("[{FileName}]", inputparam.Filename);
return Task.FromResult(aux);
})
.AfterPDF( (pdfbyte, inputparam, token) =>
{
if (inputparam is null)
{
return Task.FromResult(string.Empty);
}
//TODO : performs writing to file after performing conversion
return Task.FromResult(inputparam.Filename);
})
.Run(token);

//send result to client

Expand Down Expand Up @@ -284,7 +338,7 @@ var PDFserver = HostApp!.Services.GetHtmlPdfService();

//Performs conversion and custom operations on the server
var pdfresult = await PDFserver
.Source()
.ScopeData()
.FromHtml(HtmlSample(),5000)
.Run(applifetime.ApplicationStopping);

Expand Down
Binary file modified docs/images/swimlanes.io.AnyProcess.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/swimlanes.io.Http.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/swimlanes.io.OnlyServer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions docs/swimlanes.io.AnyProcess.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ HtmlPdfServer -> App Server: AddHtmlPdfService
App Server --> App Server : Warmup HtmlPdfService
App Client --> HtmlPdfClient: FromHtml
HtmlPdfClient --> HtmlPdfClient : Minify Html
App Client --> HtmlPdfClient: FromRazor(Optional)
App Client --> HtmlPdfClient: FromRazor
HtmlPdfClient --> HtmlPdfClient : Execute Razor Engine
HtmlPdfClient --> HtmlPdfClient : Minify Html
App Client --> HtmlPdfClient: FromUrl

App Client --> HtmlPdfClient: PageConfig
App Client --> HtmlPdfClient: Timeout
App Client -> HtmlPdfClient: ** Run (Optional Input param) **
HtmlPdfClient --> HtmlPdfClient : Create and Compress RequestHtmlPdf in Base64
HtmlPdfClient --> HtmlPdfClient : Create and Compress RequestHtmlPdf
HtmlPdfClient --> Func.Submit : Execute Submit

Func.Submit-> App Server : ** Any process **

App Server --> HtmlPdfServer: BeforePDF
App Server --> HtmlPdfServer: AfterPDF
App Server -> HtmlPdfServer: ** Run (Compress Base64) **
App Server -> HtmlPdfServer: ** Run (Compress) **

HtmlPdfServer --> HtmlPdfServer : Uncompress Base64 to RequestHtmlPdf
HtmlPdfServer --> HtmlPdfServer : Uncompress to RequestHtmlPdf
HtmlPdfServer --> HtmlPdfServer : Exec BeforePdf (Input param)
HtmlPdfServer --> HtmlPdfServer : **Generate PDF**
HtmlPdfServer --> HtmlPdfServer : Exec AfterPdf (Input param and Transform output type)
Expand Down
9 changes: 5 additions & 4 deletions docs/swimlanes.io.Http.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ HtmlPdfServer -> App Server: AddHtmlPdfService
App Server --> App Server : Warmup HtmlPdfService
App Client --> HtmlPdfClient: FromHtml
HtmlPdfClient --> HtmlPdfClient : Minify Html
App Client --> HtmlPdfClient: FromRazor(Optional)
App Client --> HtmlPdfClient: FromRazor
HtmlPdfClient --> HtmlPdfClient : Execute Razor Engine
HtmlPdfClient --> HtmlPdfClient : Minify Html
App Client --> HtmlPdfClient: FromUrl
App Client --> HtmlPdfClient: PageConfig
App Client --> HtmlPdfClient: Timeout
App Client -> HtmlPdfClient: ** Run (Optional Input param) **
HtmlPdfClient --> HtmlPdfClient : Create and Compress RequestHtmlPdf in Base64
HtmlPdfClient --> HtmlPdfClient : Create and Compress RequestHtmlPdf
HtmlPdfClient -> App Server : ** HTTP/POST **

App Server --> HtmlPdfServer: BeforePDF
App Server --> HtmlPdfServer: AfterPDF
App Server -> HtmlPdfServer: ** Run (Compress Base64) **
App Server -> HtmlPdfServer: ** Run (Compress) **

HtmlPdfServer --> HtmlPdfServer : Uncompress Base64 to RequestHtmlPdf
HtmlPdfServer --> HtmlPdfServer : Uncompress to RequestHtmlPdf
HtmlPdfServer --> HtmlPdfServer : Exec BeforePdf (Input param)
HtmlPdfServer --> HtmlPdfServer : **Generate PDF**
HtmlPdfServer --> HtmlPdfServer : Exec AfterPdf (Input param and Transform output type)
Expand Down
30 changes: 18 additions & 12 deletions docs/swimlanes.io.OnlyServer.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
title: Generate PDF Client-Sever via only server
title: Generate PDF Client-Sever via any process

note: Compress disabled (via DisableOptionsHtmlToPdf)
note: Minify, Compress and Logging can be disabled (via DisableOptionsHtmlToPdf)

HtmlPdfServer ->App Client: AddHtmlPdfService (Optional Default PageConfig)
App Client --> App Client : Warmup HtmlPdfService
App Client --> Request HtmlPdf: Html + PageConfig(Optional) + timeout
Request HtmlPdf --> Request HtmlPdf : Minify Html


App Client -> HtmlPdfServer: ** Run (Optional Input param) **
HtmlPdfServer -> App Server: AddHtmlPdfService
App Server --> App Server : Warmup HtmlPdfService
App Server --> HtmlPdfServer: FromHtml (Disable Compress)
HtmlPdfServer --> HtmlPdfServer : Minify Html
App Server --> HtmlPdfServer: FromRazor (Disable Compress)
HtmlPdfServer --> HtmlPdfServer : Execute Razor Engine
HtmlPdfServer --> HtmlPdfServer : Minify Html
App Server --> HtmlPdfServer: FromUrl (Disable Compress and Minify )
App Server --> HtmlPdfServer: Input param (opc)
App Server --> HtmlPdfServer: Timeout (opc)
App Server --> HtmlPdfServer: PageConfig (opc)
App Server --> HtmlPdfServer: BeforePDF (opc)
App Server --> HtmlPdfServer: AfterPDF (opc)
App Server -> HtmlPdfServer: ** Run **

HtmlPdfServer --> HtmlPdfServer : Exec BeforePdf (Input param)
HtmlPdfServer --> HtmlPdfServer : **Generate PDF**
HtmlPdfServer --> HtmlPdfServer : Exec AfterPdf (Input param and Transform output type)
HtmlPdfServer -> App Server : ** Result HtmlPdf**

HtmlPdfServer -> App Client : ** Result HtmlPdf**

Order: App Client, Request HtmlPdf, HtmlPdfServer
Order: App Server, HtmlPdfServer
10 changes: 5 additions & 5 deletions samples/ConsoleHtmlToPdfPlus.ClientSendHttp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public static async Task Main(string[] args)
Console.WriteLine($"HtmlPdfClient send Url to PDF Server via http post");

pdfresult = await HtmlPdfClient.Create("HtmlPdfPlusClient")
.PageConfig((cfg) => cfg.Margins(10))
.Logger(HostApp.Services.GetService<ILogger<Program>>())
.FromUrl(new Uri("https://github.com/FRACerqueira/HtmlPdfPlus"))
.Timeout(15000)
.Run(clienthttp, applifetime.ApplicationStopping);
.PageConfig((cfg) => cfg.Margins(10))
.Logger(HostApp.Services.GetService<ILogger<Program>>())
.FromUrl(new Uri("https://github.com/FRACerqueira/HtmlPdfPlus"))
.Timeout(15000)
.Run(clienthttp, applifetime.ApplicationStopping);

Console.WriteLine($"HtmlPdfClient IsSuccess {pdfresult.IsSuccess} after {pdfresult.ElapsedTime}");

Expand Down
16 changes: 9 additions & 7 deletions samples/ConsoleHtmlToPdfPlus.ClientSendTcp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ***************************************************************************************

using System.Text;
using System.Text.Json;
using HtmlPdfPlus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -20,9 +21,9 @@ public class Program
private static readonly SimpleTcpClient ClientTcp = new("127.0.0.1:9000");
private static readonly SemaphoreSlim SemaphoreSlim = new(1);
private static readonly List<byte> ResponseTcp = [];
private static string? ResultTcp= null;
private static byte[]? ResultTcp= null;
private static readonly object Lockdatareceiver = new();
private static int TimeoutWaitResponse = 30000;
private static readonly int TimeoutWaitResponse = 30000;

public static async Task Main(string[] args)
{
Expand Down Expand Up @@ -62,7 +63,7 @@ public static async Task Main(string[] args)
})
.Logger(HostApp.Services.GetService<ILogger<Program>>())
.FromHtml(HtmlSample())
.Timeout(5000)
.Timeout(500000)
.Run(SendToTcpServer, applifetime.ApplicationStopping);

Console.WriteLine($"HtmlPdfClient IsSuccess {pdfresult.IsSuccess} after {pdfresult.ElapsedTime}");
Expand Down Expand Up @@ -90,7 +91,8 @@ private static void DataReceivedTcp(object? sender, DataReceivedEventArgs e)
ResponseTcp.AddRange(e.Data.Array!);
if (ResponseTcp[^1] == 0) //token end message
{
ResultTcp = Encoding.UTF8.GetString([.. ResponseTcp], 0, ResponseTcp.Count - 1);
ResponseTcp.RemoveAt(ResponseTcp.Count - 1);
ResultTcp = [.. ResponseTcp];
ResponseTcp.Clear();
SemaphoreSlim.Release();
}
Expand All @@ -107,7 +109,7 @@ private static void ConnectedTcp(object? sender, ConnectionEventArgs e)
Console.WriteLine($"*** Server {e.IpPort} connected");
}

private static async Task<HtmlPdfResult<byte[]>> SendToTcpServer(string requestdata, CancellationToken token)
private static async Task<HtmlPdfResult<byte[]>> SendToTcpServer(byte[] requestdata, CancellationToken token)
{
// This code mybe not efficient, just to demonstrate the functionality of HtmltoPdfPlus
try
Expand All @@ -121,9 +123,9 @@ private static async Task<HtmlPdfResult<byte[]>> SendToTcpServer(string requestd
cts.CancelAfter(TimeoutWaitResponse);
//wait response to tcpserver (trigger by DataReceivedTcp release enter Semaphore)
await SemaphoreSlim.WaitAsync(TimeoutWaitResponse, token);
return ResultTcp!.ToHtmlPdfResult();
var aux = JsonSerializer.Deserialize<HtmlPdfResult<byte[]>>(Encoding.UTF8.GetString(ResultTcp!))!;
return aux.DecompressOutputData();
}

catch (Exception ex)
{
return new HtmlPdfResult<byte[]>(false, false, TimeSpan.Zero, [], ex);
Expand Down
2 changes: 1 addition & 1 deletion samples/ConsoleHtmlToPdfPlus.OnlyAtServer/v1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task Main(string[] args)

//Performs conversion and custom operations on the server
var pdfresult = await PDFserver
.Source(Path.Combine(PathToSamples, "html2pdfHtml.pdf"))
.ScopeData(Path.Combine(PathToSamples, "html2pdfHtml.pdf"))
.FromHtml(HtmlSample(), 5000)
.BeforePDF((html, _, _) =>
{
Expand Down
7 changes: 4 additions & 3 deletions samples/ConsoleHtmlToPdfPlus.OnlyAtServer/v2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task Main(string[] args)

//Performs conversion on the server
var pdfresult = await PDFserver
.Source()
.ScopeData()
.FromHtml(HtmlSample(),5000)
.Run(applifetime.ApplicationStopping);

Expand All @@ -53,7 +53,7 @@ public static async Task Main(string[] args)

//Performs conversion on the server
pdfresult = await PDFserver
.Source()
.ScopeData()
.FromUrl(new Uri("https://github.com/FRACerqueira/HtmlPdfPlus"), 5000)
.Run(applifetime.ApplicationStopping);

Expand Down Expand Up @@ -89,7 +89,8 @@ private static IHostBuilder CreateHostBuilder(string[] args) =>
{
services.AddHtmlPdfService((cfg) =>
{
cfg.Logger(LogLevel.Debug, "MyPDFServer")
cfg.DisableFeatures(HtmlPdfPlus.DisableOptionsHtmlToPdf.DisableCompress)
.Logger(LogLevel.Debug, "MyPDFServer")
.DefaultConfig((page) =>
{
page.DisplayHeaderFooter(true)
Expand Down
Loading