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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29025.244
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E602293-DA16-41FE-AB74-00AAD6C496B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B291216-3FD5-4017-BFA8-874021F734CF}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
RUN mkdir -p /app/input
RUN apt-get update && \
apt-get install -y libfontconfig1 && \
rm -rf /var/lib/apt/lists/*
COPY "jQuery_Succinctly.pdf" /app/input/
RUN chmod -R 755 /app/input/
USER $APP_UID
WORKDIR /app

#docker cp "jQuery_Succinctly.pdf"


# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["PDFCompression/PDFCompression.csproj", "PDFCompression/"]
RUN dotnet restore "./PDFCompression/PDFCompression.csproj"
COPY . .
WORKDIR "/src/PDFCompression"
RUN dotnet build "./PDFCompression.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./PDFCompression.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PDFCompression.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>b67e9578-acf6-4e63-bb4a-4f5359ec7803</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<None Remove="jQuery_Succinctly.pdf" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="*.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.IO;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;

namespace PDFCompression;

public class Program
{
public static void Main(string[] args)
{
CompressPdf();
}

public static void CompressPdf()
{
string path = Path.GetFullPath(@"Input.pdf");
Console.WriteLine($"'{path}' pdf compression started.");

Stopwatch stopwatch = Stopwatch.StartNew();
FileStream inputDocument = new FileStream(path, FileMode.Open, FileAccess.Read);
Console.WriteLine($"Original file size: {inputDocument.Length:N0}");

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);

// Create a new compression option.
PdfCompressionOptions options = new PdfCompressionOptions
{
// Enable image compression.
CompressImages = true,
// Set the image quality.
ImageQuality = 30,
// Optimize fonts in the PDF document.
OptimizeFont = true,
// Optimize page contents.
OptimizePageContents = true,
// Remove metadata from the PDF document.
RemoveMetadata = true
};

// Compress the PDF document.
loadedDocument.Compress(options);

// Save the PDF document.
MemoryStream outputDocument = new MemoryStream();

// Save the PDF document to memory stream.
loadedDocument.Save(outputDocument);

stopwatch.Stop();
Console.WriteLine($"Compressed file size: {outputDocument.Length:N0}");
Console.WriteLine($"Time elapsed: {stopwatch.Elapsed}");

// Clean up resources.
outputDocument.Close();
loadedDocument.Close(true);
inputDocument.Close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"PDFCompression": {
"commandName": "Project"
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
}
}
Loading