Skip to content

Commit 794876a

Browse files
Merge pull request #166 from SyncfusionExamples/959881
959881 Added compression PDF using Linux docker
2 parents 07d31ac + f374e8b commit 794876a

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29025.244
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Linux-Based-PDF-Compression-Using-Docker", "Linux-Based-PDF-Compression-Using-Docker\Linux-Based-PDF-Compression-Using-Docker.csproj", "{8E602293-DA16-41FE-AB74-00AAD6C496B6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {2B291216-3FD5-4017-BFA8-874021F734CF}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
# This stage is used when running from VS in fast mode (Default for Debug configuration)
4+
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
5+
RUN mkdir -p /app/input
6+
RUN apt-get update && \
7+
apt-get install -y libfontconfig1 && \
8+
rm -rf /var/lib/apt/lists/*
9+
COPY "jQuery_Succinctly.pdf" /app/input/
10+
RUN chmod -R 755 /app/input/
11+
USER $APP_UID
12+
WORKDIR /app
13+
14+
#docker cp "jQuery_Succinctly.pdf"
15+
16+
17+
# This stage is used to build the service project
18+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
19+
ARG BUILD_CONFIGURATION=Release
20+
WORKDIR /src
21+
COPY ["PDFCompression/PDFCompression.csproj", "PDFCompression/"]
22+
RUN dotnet restore "./PDFCompression/PDFCompression.csproj"
23+
COPY . .
24+
WORKDIR "/src/PDFCompression"
25+
RUN dotnet build "./PDFCompression.csproj" -c $BUILD_CONFIGURATION -o /app/build
26+
27+
# This stage is used to publish the service project to be copied to the final stage
28+
FROM build AS publish
29+
ARG BUILD_CONFIGURATION=Release
30+
RUN dotnet publish "./PDFCompression.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
31+
32+
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
33+
FROM base AS final
34+
WORKDIR /app
35+
COPY --from=publish /app/publish .
36+
ENTRYPOINT ["dotnet", "PDFCompression.dll"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<UserSecretsId>b67e9578-acf6-4e63-bb4a-4f5359ec7803</UserSecretsId>
7+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<None Remove="jQuery_Succinctly.pdf" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
16+
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />
17+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Update="*.pdf">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<None Update="Data\Input.pdf">
28+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29+
</None>
30+
</ItemGroup>
31+
32+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
4+
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
5+
</PropertyGroup>
6+
<PropertyGroup>
7+
<ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile>
8+
</PropertyGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using Syncfusion.Pdf.Parsing;
5+
using Syncfusion.Pdf;
6+
7+
namespace PDFCompression;
8+
9+
public class Program
10+
{
11+
public static void Main(string[] args)
12+
{
13+
CompressPdf();
14+
}
15+
16+
public static void CompressPdf()
17+
{
18+
string path = Path.GetFullPath(@"Input.pdf");
19+
Console.WriteLine($"'{path}' pdf compression started.");
20+
21+
Stopwatch stopwatch = Stopwatch.StartNew();
22+
FileStream inputDocument = new FileStream(path, FileMode.Open, FileAccess.Read);
23+
Console.WriteLine($"Original file size: {inputDocument.Length:N0}");
24+
25+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);
26+
27+
// Create a new compression option.
28+
PdfCompressionOptions options = new PdfCompressionOptions
29+
{
30+
// Enable image compression.
31+
CompressImages = true,
32+
// Set the image quality.
33+
ImageQuality = 30,
34+
// Optimize fonts in the PDF document.
35+
OptimizeFont = true,
36+
// Optimize page contents.
37+
OptimizePageContents = true,
38+
// Remove metadata from the PDF document.
39+
RemoveMetadata = true
40+
};
41+
42+
// Compress the PDF document.
43+
loadedDocument.Compress(options);
44+
45+
// Save the PDF document.
46+
MemoryStream outputDocument = new MemoryStream();
47+
48+
// Save the PDF document to memory stream.
49+
loadedDocument.Save(outputDocument);
50+
51+
stopwatch.Stop();
52+
Console.WriteLine($"Compressed file size: {outputDocument.Length:N0}");
53+
Console.WriteLine($"Time elapsed: {stopwatch.Elapsed}");
54+
55+
// Clean up resources.
56+
outputDocument.Close();
57+
loadedDocument.Close(true);
58+
inputDocument.Close();
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"PDFCompression": {
4+
"commandName": "Project"
5+
},
6+
"Container (Dockerfile)": {
7+
"commandName": "Docker"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)