Skip to content

Commit

Permalink
Fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Rivera committed Aug 29, 2020
1 parent a31fe08 commit 1ad3479
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 5 deletions.
19 changes: 19 additions & 0 deletions PdfMakeNet.Server.Extensions/PdfMakeNet.Server.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageReleaseNotes>Implemented SVG's and WaterMark. Removed Microsoft.AspNetCore.Mvc dependency to another project as server side only extensions.</PackageReleaseNotes>
<AssemblyVersion>0.1.0.5</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Authors>Anthony G. Rivera Cosme</Authors>
<Description>PdfMakeNet.Server.Extension is a PdfMakeNet helper extensions for server side only.</Description>
<Copyright>Anthony G. Rivera Cosme</Copyright>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageProjectUrl>https://github.com/arivera12/PdfMakeNet</PackageProjectUrl>
<RepositoryUrl>https://github.com/arivera12/PdfMakeNet</RepositoryUrl>
<Version>1.0.5</Version>
<FileVersion>0.1.0.5</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,4 +24,11 @@
<ProjectReference Include="..\PdfMakeNet\PdfMakeNet.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\PdfMakeNet\LICENSE.txt">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions PdfMakeNet/Bases/PdfMake/PdfMake.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Newtonsoft.Json;
using PdfMakeNet.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace PdfMakeNet
{
Expand Down Expand Up @@ -706,5 +711,69 @@ public void AddFooterLink(IPdfMakeLink pdfMakeLink)
{
Footer.Add(pdfMakeLink);
}
/// <summary>
/// Reads an Image from an http request and converts it to its base 64 representation with its data uri format ready to be embed.
/// This method specs that the resource is an image type compatible with pdfmake.
/// JPEG and PNG formats are supported.
/// </summary>
/// <param name="httpClient"></param>
/// <param name="httpMethod"></param>
/// <param name="requestUri"></param>
/// <returns></returns>
public static async Task<string> LoadImageFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri, SourceImageFormat sourceImageFormat)
{
var request = new HttpRequestMessage(new HttpMethod(httpMethod), requestUri);
var response = await httpClient.SendAsync(request);
var stream = await response.Content.ReadAsStreamAsync();
var byteArray = await stream.ToByteArrayAsync();
var base64StringImage = Convert.ToBase64String(byteArray);
return $"data:{sourceImageFormat.GetMimeType()};base64,{base64StringImage}";
}
/// <summary>
/// Reads an Image from an http request and converts it to its base 64 representation with its data uri format ready to be embed.
/// This method specs that the resource is an image type compatible with pdfmake.
/// JPEG and PNG formats are supported.
/// </summary>
/// <param name="httpClient"></param>
/// <param name="httpMethod"></param>
/// <param name="requestUri"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<string> LoadImageFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri, SourceImageFormat sourceImageFormat, CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(new HttpMethod(httpMethod), requestUri);
var response = await httpClient.SendAsync(request);
var stream = await response.Content.ReadAsStreamAsync();
var byteArray = await stream.ToByteArrayAsync(cancellationToken);
var base64StringImage = Convert.ToBase64String(byteArray);
return $"data:{sourceImageFormat.GetMimeType()};base64,{base64StringImage}";
}
/// <summary>
/// Reads an svg text from an http request.
/// </summary>
/// <param name="httpClient"></param>
/// <param name="httpMethod"></param>
/// <param name="requestUri"></param>
/// <returns></returns>
public static async Task<string> LoadSvgFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri)
{
var request = new HttpRequestMessage(new HttpMethod(httpMethod), requestUri);
var response = await httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Reads an image from the system file and converts it to its base 64 representation with its data uri format ready to be embed.
/// This method specs that the resource is an image type compatible with pdfmake.
/// JPEG and PNG formats are supported.
/// </summary>
/// <param name="path"></param>
/// <param name="sourceImageFormat"></param>
/// <returns></returns>
public static string LoadImageFromPath(string path, SourceImageFormat sourceImageFormat)
{
var byteArray = File.ReadAllBytes(path);
var base64StringImage = Convert.ToBase64String(byteArray);
return $"data:{sourceImageFormat.GetMimeType()};base64,{base64StringImage}";
}
}
}
8 changes: 8 additions & 0 deletions PdfMakeNet/Enums/SourceImageFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PdfMakeNet
{
public enum SourceImageFormat
{
JPEG,
PNG
}
}
18 changes: 18 additions & 0 deletions PdfMakeNet/Extensions/SourceImageFormatExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace PdfMakeNet.Extensions
{
public static class SourceImageFormatExtensions
{
public static string GetMimeType(this SourceImageFormat sourceImageFormat)
{
if (sourceImageFormat == SourceImageFormat.JPEG)
{
return "image/jpeg";
}
else if (sourceImageFormat == SourceImageFormat.PNG)
{
return "image/png";
}
return null;
}
}
}
31 changes: 31 additions & 0 deletions PdfMakeNet/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace PdfMakeNet.Extensions
{
public static class StreamExtensions
{
public static byte[] ToByteArray(this Stream stream)
{
var streamLength = (int)stream.Length;
var data = new byte[streamLength];
stream.Read(data, 0, streamLength);
return data;
}
public static async Task<byte[]> ToByteArrayAsync(this Stream stream)
{
var streamLength = (int)stream.Length;
var data = new byte[streamLength];
await stream.ReadAsync(data, 0, streamLength);
return data;
}
public static async Task<byte[]> ToByteArrayAsync(this Stream stream, CancellationToken cancellationToken)
{
var streamLength = (int)stream.Length;
var data = new byte[streamLength];
await stream.ReadAsync(data, 0, streamLength, cancellationToken);
return data;
}
}
}
10 changes: 5 additions & 5 deletions PdfMakeNet/PdfMakeNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Authors>Anthony G. Rivera Cosme</Authors>
<Product>Anthony G. Rivera Cosme</Product>
<Product>PdfMakeNet</Product>
<Company />
<Description>PdfMakeNet is a pdfmake wrapper in c#</Description>
<Copyright>Anthony G. Rivera Cosme</Copyright>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageProjectUrl>https://github.com/arivera12/PdfMakeNet</PackageProjectUrl>
<RepositoryUrl>https://github.com/arivera12/PdfMakeNet</RepositoryUrl>
<PackageReleaseNotes>BackgroundColor and Color were rolled back to string cause newtonsoft json have issues serializing structs.</PackageReleaseNotes>
<PackageReleaseNotes>Implemented SVG's and WaterMark. Removed Microsoft.AspNetCore.Mvc dependency to another project as server side only extensions.</PackageReleaseNotes>
<NeutralLanguage>en-US</NeutralLanguage>
<AssemblyVersion>0.1.0.4</AssemblyVersion>
<FileVersion>0.1.0.4</FileVersion>
<AssemblyVersion>0.1.0.5</AssemblyVersion>
<FileVersion>0.1.0.5</FileVersion>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1ad3479

Please sign in to comment.