Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: PDF generation #220

Merged
merged 18 commits into from
Apr 23, 2024
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
3 changes: 2 additions & 1 deletion src/BinaryKits.Zpl.Label/Elements/ZplGraphicCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public ZplGraphicCircle(
int diameter,
int borderThickness = 1,
LineColor lineColor = LineColor.Black,
bool reversePrint = false,
bool bottomToTop = false)
: base(positionX, positionY, borderThickness, lineColor, bottomToTop: bottomToTop)
: base(positionX, positionY, borderThickness, lineColor, reversePrint, bottomToTop)
{
Diameter = diameter;
}
Expand Down
8 changes: 5 additions & 3 deletions src/BinaryKits.Zpl.Labelary/LabelSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public double WidthInInch
return _width;
}

return Math.Round(_width / _millimeterToInch, 0);
var rounded = Math.Round(_width / _millimeterToInch, 8);
return rounded;
}
}

Expand All @@ -37,8 +38,9 @@ public double HeightInInch
{
return _height;
}

return Math.Round(_height / _millimeterToInch, 0);

var rounded = Math.Round(_height / _millimeterToInch, 8);
return rounded;
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/BinaryKits.Zpl.Labelary/LabelaryClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Globalization;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,9 +43,15 @@ public async Task<byte[]> GetPreviewAsync(
{
var dpi = printDensity.ToString().Substring(2);
var zpl = Encoding.UTF8.GetBytes(zplData);

//without setting this, a comma separator might be used for for the size numbers in the URL
var specifier = "G";
var culture = CultureInfo.CreateSpecificCulture("en-US");
var width = labelSize.WidthInInch.ToString(specifier, culture);
var height = labelSize.HeightInInch.ToString(specifier, culture);

using var byteContent = new ByteArrayContent(zpl);
using (var response = await _httpClient.PostAsync($"{_apiEndpoint}/{dpi}/labels/{labelSize.WidthInInch}x{labelSize.HeightInInch}/0/", byteContent))
using (var response = await _httpClient.PostAsync($"{_apiEndpoint}/{dpi}/labels/{width}x{height}/0/", byteContent))
{
if (!response.IsSuccessStatusCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<None Update="Labels\Example\Example1-54x86.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example3-102x152.zpl2">
<None Update="Labels\Example\Example3-54x86.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Test\Barcode128-102x152.zpl2">
Expand Down Expand Up @@ -69,16 +69,16 @@
<None Update="Labels\Example\Example4-102x152.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example7-102x152.zpl2">
<None Update="Labels\Example\Example7-178x152.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example6-102x152.zpl2">
<None Update="Labels\Example\Example6-75x254.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example5-102x152.zpl2">
<None Update="Labels\Example\Example5-75x202.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example8-102x152.zpl2">
<None Update="Labels\Example\Example8-64x152.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example2-102x170.zpl2">
Expand Down Expand Up @@ -129,7 +129,13 @@
<None Update="Labels\Test\BarcodePDF417-102x152.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Test\GS1-102x152.zpl2">
<None Update="Labels\Test\FieldReversePrint3-54x86.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example10-102x152.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Labels\Example\Example11-102x152.zpl2">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
58 changes: 51 additions & 7 deletions src/BinaryKits.Zpl.Viewer.WebApi/Controllers/ViewerController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BinaryKits.Zpl.Viewer.WebApi.Models;
using BinaryKits.Zpl.Viewer.ElementDrawers;
using BinaryKits.Zpl.Viewer.WebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -34,25 +35,68 @@ public ActionResult<RenderResponseDto> Render(RenderRequestDto request)
private ActionResult<RenderResponseDto> RenderZpl(RenderRequestDto request)
{
IPrinterStorage printerStorage = new PrinterStorage();
var drawer = new ZplElementDrawer(printerStorage);
var drawerOptions = new DrawerOptions();
drawerOptions.OpaqueBackground = true; //set white background for viewer requests

//PDF mode (image mode is default)
if (request.Type == "PDF")
{
drawerOptions.PdfOutput = true;
}

var drawer = new ZplElementDrawer(printerStorage, drawerOptions);

var analyzer = new ZplAnalyzer(printerStorage);
var analyzeInfo = analyzer.Analyze(request.ZplData);

var labels = new List<RenderLabelDto>();
var pdfs = new List<RenderLabelDto>();
foreach (var labelInfo in analyzeInfo.LabelInfos)
{
var imageData = drawer.Draw(labelInfo.ZplElements, request.LabelWidth, request.LabelHeight, request.PrintDensityDpmm);
var label = new RenderLabelDto
if (request.Type == "image")
{
var imageData = drawer.Draw(labelInfo.ZplElements, request.LabelWidth, request.LabelHeight, request.PrintDensityDpmm);
var label = new RenderLabelDto
{
ImageBase64 = Convert.ToBase64String(imageData)
};
labels.Add(label);
}

if (request.Type == "PDF")
{
var pdfData = drawer.DrawPdf(labelInfo.ZplElements, request.LabelWidth, request.LabelHeight, request.PrintDensityDpmm);
var pdf = new RenderLabelDto
{
PdfBase64 = Convert.ToBase64String(pdfData)
};
pdfs.Add(pdf);
}

if (request.Type == "both")
{
ImageBase64 = Convert.ToBase64String(imageData)
};
labels.Add(label);
var bothData = drawer.DrawMulti(labelInfo.ZplElements, request.LabelWidth, request.LabelHeight, request.PrintDensityDpmm);

var imageData = bothData[0];
var label = new RenderLabelDto
{
ImageBase64 = Convert.ToBase64String(imageData)
};
labels.Add(label);

var pdfData = bothData[1];
var pdf = new RenderLabelDto
{
PdfBase64 = Convert.ToBase64String(pdfData)
};
pdfs.Add(pdf);
}
}

var response = new RenderResponseDto
{
Labels = labels.ToArray(),
Pdfs = pdfs.ToArray(),
NonSupportedCommands = analyzeInfo.UnknownCommands
};

Expand Down
109 changes: 109 additions & 0 deletions src/BinaryKits.Zpl.Viewer.WebApi/Labels/Example/Example10-102x152.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
^XA
^CI28
^FO560,30^GFA,924,924,28,
O01PFEK03JFC00KF01KF,O01QFEJ07JFC01JFE03KF,O03RFJ07JF803JFE07JFE,
O07RF8I0KF007JFC0KFC,O0SFC001KF007JF80KF8,O0SFE003JFE00KF01KF8,
N01SFE003JFC01KF03KF,N03TF007JF803JFE03JFE,N07TF00KF803JFC07JFC,
N07TF00KF007JFC0KFC,X0KF01JFE00KF81KF8,X07IFE03RF01KF,
X0JFE03QFE03KF,P01JFE01JFC07QFE07JFE,P03JFE03JF80RFC07JFC,
P03JFC03JF81RF80KFC,P07JF807JF01RF81KF8,P0KF00JFE03RF01KF,
P0KF00JFE07QFE03JFE,O01JFE,O03JFC,O07JFE,
NF87QF03JFE00KF01QF87KFENF0QFE03JFC01KF03QF8LFEN01QFC07JF803JFE07QF,
N01QF80KF803JFC07PFE,
MFC3QF01KF007JF80QFE3LFEMF87PFE01JFE00KF80QFC7LFEN0QFC03JFC00KF00QF8,
N0QF007JFC01JFE007PF,L021PFC007JF803JFC007PF,
LFE3PFI0KF007JFC001OFE1MFE7KFC3NFEI01JFE007JF8I01NFC3MFC^FS


Horizontal line

^FO0000,0075^GB0799,0000,0002^FS Lijn 1
^FO0000,0138^GB0799,0000,0002^FS Lijn 2
^FO0000,0383^GB0799,0000,0002^FS Lijn 3
^FO0000,0440^GB0799,0000,0002^FS Lijn 4
^FO0000,0490^GB0799,0000,0002^FS Lijn 5

Vertical Line

^FO0680,0075^GB0000,0065,0002^FS Lijn 1
^FO0560,0385^GB0000,0057,0002^FS Lijn 2.0
^FO0670,0385^GB0000,0057,0002^FS Lijn 2.1
^FO0670,0431^GB0000,0059,0002^FS Lijn 3

^CW2,E:ARIBLK.FNT^FS
^CW4,E:BOOKOS.FNT^FS

^FO0200,0030^A0N,55,35^CI28^FDDHL PARCEL CONNECT^FS
^FO015,0030^A0N,55,32^CI28^FR^FDCMR^FS
^FO0100,0030^A0N,18,20^CI28^FH^FDLabel^FS
^FO0100,0050^A0N,18,20^CI28^FH^FDversion 5.7^FS
^FO0035,0070^A0N,18,20^CI28^FD^FS

TEXT FROM START
^A0N,22,22^FO35,85^CI28^FDFROM:^FS
^A0N,25,25^FO400,85^CI28^FDContact:^FS VAR GEADRESEERDE FROM
^A0N,25,25^FO400,108^CI28^FD^FS VAR GEADRESEERDE FROM
^A0N,20,22^FO100,85^CI28^FDSome company^FS VAR SENDERS NAME
^A0N,20,22^FO100,103^CI28^FDSome street 10^FS VAR SENDERS STREET AND HOUSENUMBER
^A0N,20,22^FO145,121^CI28^FD9999 AA Someplace^FS VAR SENDERS POSTCODE AND CITY
^A0N,20,22^FO100,121^CI28^FDNL-^FS VAR SENDERS COUNTRYCODE
TEXT FROM END

^FO100,165^GB75,5,3^FS HAAK LINKSBOVEN
^FO100,165^GB5,75,3^FS HAAK LINKSBOVEN
^FO625,165^GB75,5,3^FS HAAK RECHTSBOVEN
^FO700,165^GB5,75,3^FS HAAK RECHTSBOVEN
^FO100,355^GB75,5,3^FS HAAK LINKSONDER
^FO100,285^GB5,75,3^FS HAAK LINKSONDER
^FO625,355^GB75,5,3^FS HAAK RECHTSONDER
^FO700,285^GB5,75,3^FS HAAK RECHTSONDER

TEXT TO START
^A0N,27,30^FO165,205^CI28^FDTO:^FS
^A0N,27,30^FO210,205^CI28^FD^FS VAR DELIVERY NAAM/STREET/HOUSENUMBER
^A0N,27,30^FO210,230^CI28^FDMevr. Some Persone^FS VAR DELIVERY NAAM/STREET/HOUSENUMBER
^A0N,27,30^FO210,255^CI28^FDStreet 17 E^FS VAR DELIVERY NAAM/STREET/HOUSENUMBER
^A0N,27,30^FO210,280^CI28^FD09999 Someplace^FS VAR DELIVERY POSTCODE/CITY/COUNTRY
^A0N,27,30^FO210,305^CI28^FDGermany^FS VAR DELIVERY POSTCODE/CITY/COUNTRY
TEXT TO END

^FO0688,0449^A0N,48,48^CI28^FD^FS

^FO0000,0383^GB0300,0000,0057^FS Black box
^FO25,0430^A0N,62,62^CI28^FR^FD^FS

^FO0568,0390^A0N,21,21^CI28^FDDAY:^FS VAR DAY
^FO0625,0395^A0N,50,50^CI28^FD^FS VAR CONTENT DAY

^FO0688,0390^A0N,21,21^CI28^FDTIME:^FS VAR TIME
^FO0665,0408^A0N,40,40^CI28^FD^FS VAR CONTENT TIME


^FO0020,0450^A0N,23,25^CI28^FH^FDAccount No.:^FS VAR ACCOUNT NO
^FO0170,0450^A0N,23,25^CI28^FH^FD012346578^FS VAR CONTENT ACCOUNT NO

^FO0020,470^A0N,23,25^CI28^FDShipment ref.: ^FS VAR SHIPMENT REF
^FO170,470^A0N,23,25^CI28^FD1235456879^FS VAR CONTENT SHIPMENT REF

^FO0350,0450^A0N,23,25^CI28^FDPackage Weight:^FS VAR PACKAGE WEIGHT
^FO0520,0450^A0N,23,25^CI28^FH^FD2,00 KGM^FS VAR CONTENT PACKAGE WEIGHT
^FO0350,0470^A0N,23,25^CI28^FDPickup date: ^FS VAR PICKUP DATE
^FO0520,0470^A0N,23,25^CI28^FH^FD20249999^FS VAR CONTENT PICKUP DATE

^FO0675,0447^A0N,23,25^CI28^FDPiece:^FS VAR PIECE
^FO0735,0450^A0N,45,45^CI28^FD1/1^FS VAR CONTENT PIECE


PDF BARCODE START
^FO0100,0520^BY2^B7N,6.7,5,,30,N^FD
UNH+2876965+IFTMIN:D:96B:UN+DHL5.6.0/AWESOME'BGM+787+999994760001+9'DTM+186:20240312:102'TSR+++10'TOD+Z02++CPT'NAD+OS+0464651'NAD+CN+++MEVR. SOME PERSONE+STREET 17 E+SOMEPLACE++0+DE'CTA+GR'[email protected]:EM'GID+0+1'MEA+WT++KGM:2'PCI+ZZ1+JVGL09999999999994760001'UNT+13+123546798'^FS
PDF BARCODE END

^BY3^FO100,0720^BCN,200,N,N,N^FD2LDE0+>570000000^FS VAR ROUTING BARCODE
^A0N,20,22^FO287,925^CI28^FD2LDE0+70000000^FS VAR TEXT UNDER ROUTING BARCODE

^BY3^FO100,0955^BCN,200,N,N,N^FDJVGL>509999999999994760001^FS VAR LICENCE PLATE
^A0N,20,22^FO287,1160^CI28^FDJVGL09999999999994760001^FS VAR TEXT UNDER LICENCE PLATE
^PQ1,0,0,

^XZ
102 changes: 102 additions & 0 deletions src/BinaryKits.Zpl.Viewer.WebApi/Labels/Example/Example11-102x152.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
^XA
^PMN
^MUD
^LH15,0
^CI28
^JMA
^FO10,946,^GB616,137,0^FS
^FO10,703,^GB616,244,0^FS
^FO24,712,^A0N,24^FR^FD^FS
^FO24,743,^A0N,43^FR^FDJS Corporation^FS
^FO24,790,^A0N,31^FR^FDSales Department^FS
^FO24,824,^A0N,31^FR^FDc/o John Smith^FS
^FO24,857,^A0N,43^FR^FDImaginary Drive 25^FS
^FO24,903,^A0N,31^FR^FDGB^FS
^FO256,716,^A0N,18^FR^FDCustomer No^FS
^FO379,716,^A0N,18^FR^FD^FS
^FO112,1052,^A0N,24^FR^FD^FS
^FO18,953,^A0N,24^FR^FDContact^FS
^FO18,978,^A0N,24^FR^FDPhone^FS
^FO18,1003,^A0N,24^FR^FDNote^FS
^FO112,953,^A0N,24^FR^FD^FS
^FO112,978,^A0N,24^FR^FD^FS
^FO112,1003,^A0N,24^FR^FD^FS
^FO112,1028,^A0N,24^FR^FD^FS
^FO10,444,^GB616,260,0^FS
^FO18,1028,^A0N,24^FR^FDNote^FS
^FO18,1052,^A0N,24^FR^FDRef.No^FS
^FO625,444,^GB162,639,0^FS
^FO24,453,^A0N,42^FR^FD^FS
^FO24,502,^A0N,24^FR^FD^FS
^FO24,527,^A0N,24^FR^FD^FS
^FO24,556,^A0N,24^FR^FD^FS
^FO24,580,^A0N,32^FR^FD^FS
^FO24,628,^A0N,24^FR^FD^FS
^FO24,653,^A0N,24^FR^FD^FS
^FO24,678,^A0N,24^FR^FD^FS
^FO100,903,^A0N,31^FR^FDMK7 8LE^FS
^FO285,903,^A0N,31^FR^FDJohn Doe^FS
^FO630,514,^A0R,18^FR^FD12345^FS
^FO630,607,^A0R,18^FR^FDNevermind^FS
^FO630,970,^A0R,50^FR^FD^FS
^FO754,451,^A0R,18^FR^FDShipper^FS
^FO727,451,^A0R,24^FR^FDSome Services GmbH^FS
^FO754,572,^A0R,18^FR^FDCust.ID^FS
^FO754,692,^A0R,18^FR^FD20000012^FS
^FO754,840,^A0R,18^FR^FDCon.ID^FS
^FO754,946,^A0R,18^FR^FD27600000aB^FS
^FO701,451,^A0R,18^FR^FD^FS
^FO677,451,^A0R,18^FR^FD^FS
^FO654,451,^A0R,18^FR^FDUTF8-Test-ÄöüÜÖÄâá-Straße 1-7^FS
^FO630,451,^A0R,18^FR^FDDE^FS
^FO570,12,^GB224,90,90^FS
^FO248,13,^GB71,89,71^FS
^FO4,5,^GB790,9,9^FS
^FO27,120,^A0N,62^FR^FD301^FS
^FO183,139,^A0N,43^FR^FDMK7 8LE^FS
^FO25,28,^A0N,74^FR^FDCVT^FS
^FO450,30,^A0N,74^FR^FDGB^FS
^FO7,102,^GB787,6,6^FS
^FO263,25,^A0N,80^FR^FD0^FS
^FO216,110,^A0N,18^FR^FDZipcode^FS
^FO384,110,^A0N,18^FR^FDYour GLS Track ID^FS
^FO387,147,^A0N,34^FR^FDABCDEF0^FS
^FO8,184,^GB787,6,6^FS
^FO8,395,^GB783,9,9^FS
^FO50,217,^BY4^BXN,4,200^FR^FDA 000062276000001227600000aBABCDEF0 0CVT0301 00100001001 ^FS
^FO595,214,^BY4^BXN,4,200^FR^FDA|Some Corporation|Imaginary Drive|John Doe|25||| ^FS
^FO8,190,^GB24,8,8^FS
^FO8,196,^GB8,16,8^FS
^FO8,372,^GB8,16,8^FS
^FO8,387,^GB24,8,8^FS
^FO229,387,^GB24,8,8^FS
^FO244,196,^GB8,16,8^FS
^FO228,190,^GB24,8,8^FS
^FO245,372,^GB8,16,8^FS
^FO580,28,^A0N,80^FR^FD0062^FS
^FO303,352,^A0N,37^FR^FD012345678900^FS
^FO720,416,^A0N,18^FR^FDF2.00.0^FS
^FO616,416,^A0N,18^FR^FD27082021^FS
^FO168,416,^A0N,18^FR^FD27.08.2021^FS
^FO32,408,^A0N,31^FR^FDDE 500^FS
^FO342,408,^A0N,37^FR^FD1.00^FS
^FO131,407,^A0N,31^FR^FD^FS
^FO273,416,^A0N,18^FR^FD10:33^FS
^FO481,416,^A0N,18^FR^FD001^FS
^FO525,416,^A0N,18^FR^FD001^FS
^FO510,416,^A0N,18^FR^FD/^FS
^FO572,416,^A0N,18^FR^FDRTG^FS
^FO268,221,^BY3,2.0,125,^B2N,125,N,N^FR^FD012345678900^FS
^FO568,135,^A0N,41^FR^FD^FS
^FO589,135,^A0N,41^FR^FD^FS
^FO654,135,^A0N,41^FR^FD^FS
^FO689,135,^A0N,41^FR^FD^FS
^FO752,135,^A0N,41^FR^FDS^FS
^FO772,135,^A0N,41^FR^FD^FS
^FO425,407,^A0N,37^FR^FDkg^FS
^FO18,1090,^A0N,20^FR^FDInformation about Data Protection in GLS Group can be found at^FS
^FO18,1115,^A0N,20^FR^FDgls-group.eu/dataprotection^FS
^FO18,1140,^A0N,20^FR^FD^FS
^FO18,1165,^A0N,20^FR^FD^FS
^PQ1
^XZ
Loading
Loading