Converter and direct BASE64 files
This commit is contained in:
parent
8269718126
commit
c693c96100
8
.gitignore
vendored
8
.gitignore
vendored
@ -5,4 +5,10 @@ PackageSources/js-bundle/.rollup.cache
|
|||||||
PackageSources/js-bundle/node_modules
|
PackageSources/js-bundle/node_modules
|
||||||
PackageSources/html_ui
|
PackageSources/html_ui
|
||||||
PackageSources/module/.vs
|
PackageSources/module/.vs
|
||||||
PackageSources/module/MSFS
|
PackageSources/module/MSFS
|
||||||
|
PackageSources/converter/bin
|
||||||
|
PackageSources/converter/obj
|
||||||
|
PackageSources/converter/.vs
|
||||||
|
|
||||||
|
PackageSources/converter/*.user
|
||||||
|
PackageSources/converter/Properties/PublishProfiles
|
||||||
|
|||||||
113
PackageSources/converter/Program.cs
Normal file
113
PackageSources/converter/Program.cs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
using Docnet.Core.Models;
|
||||||
|
using Docnet.Core.Readers;
|
||||||
|
using Docnet.Core;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using Docnet.Core.Exceptions;
|
||||||
|
|
||||||
|
#region Main Code
|
||||||
|
|
||||||
|
if (args.Length == 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("No PDF specified");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Conversion for {args[0]}");
|
||||||
|
Console.WriteLine("Using configuration");
|
||||||
|
Console.WriteLine("\tA4 at 96dpi");
|
||||||
|
Console.WriteLine();
|
||||||
|
return ReadPDF(args[0]);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Functions
|
||||||
|
|
||||||
|
int ReadPDF(string path)
|
||||||
|
{
|
||||||
|
string outPath = "";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
outPath = Path.GetFileNameWithoutExtension(path);
|
||||||
|
Directory.CreateDirectory(outPath);
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("Error creating file directory");
|
||||||
|
Console.Error.WriteLine(e.Message);
|
||||||
|
Console.ReadKey();
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(794, 1122)))
|
||||||
|
{
|
||||||
|
int pages = docReader.GetPageCount();
|
||||||
|
Console.WriteLine($"Converting {pages} pages");
|
||||||
|
|
||||||
|
for (int i = 0; i < pages; i++)
|
||||||
|
{
|
||||||
|
using (IPageReader pageReader = docReader.GetPageReader(i))
|
||||||
|
{
|
||||||
|
byte[] rawBytes = pageReader.GetImage();
|
||||||
|
int width = pageReader.GetPageWidth();
|
||||||
|
int height = pageReader.GetPageHeight();
|
||||||
|
|
||||||
|
using (Bitmap doc = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
||||||
|
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
||||||
|
{
|
||||||
|
AddBytes(bmp, rawBytes);
|
||||||
|
|
||||||
|
Graphics g = Graphics.FromImage(doc);
|
||||||
|
g.FillRegion(Brushes.White, new Region(new Rectangle(0, 0, width, height)));
|
||||||
|
g.DrawImage(bmp, new Point(0, 0));
|
||||||
|
g.Save();
|
||||||
|
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
Bitmap thumb = new Bitmap(doc, new Size(doc.Width / 10, doc.Height / 10));
|
||||||
|
using (MemoryStream stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
//saving and exporting
|
||||||
|
thumb.Save(stream, ImageFormat.Jpeg);
|
||||||
|
Console.WriteLine($"Ouputing tumbnail");
|
||||||
|
File.WriteAllText($"{outPath}\\thumb.bjpg", Convert.ToBase64String(stream.ToArray()));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
using (MemoryStream stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
//saving and exporting
|
||||||
|
doc.Save(stream, ImageFormat.Jpeg);
|
||||||
|
Console.WriteLine($"Ouputing page {i + 1}");
|
||||||
|
File.WriteAllText($"{outPath}\\{i + 1}.bjpg", Convert.ToBase64String(stream.ToArray()));
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(DocnetLoadDocumentException)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("File is not a PDF");
|
||||||
|
Console.ReadKey();
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.ReadKey();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddBytes(Bitmap bmp, byte[] rawBytes)
|
||||||
|
{
|
||||||
|
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||||
|
|
||||||
|
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);
|
||||||
|
nint pNative = bmpData.Scan0;
|
||||||
|
|
||||||
|
Marshal.Copy(rawBytes, 0, pNative, rawBytes.Length);
|
||||||
|
bmp.UnlockBits(bmpData);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
26
PackageSources/converter/converter.csproj
Normal file
26
PackageSources/converter/converter.csproj
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPublishable>False</IsPublishable>
|
||||||
|
<Trimming>full</Trimming>
|
||||||
|
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<WarningLevel>9999</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<WarningLevel>9999</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Docnet.Core" Version="2.6.0" />
|
||||||
|
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
25
PackageSources/converter/converter.sln
Normal file
25
PackageSources/converter/converter.sln
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.8.34309.116
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "converter", "converter.csproj", "{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {D95A0B4E-6516-4CBE-84E0-5FF1068CEF60}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
@ -17,7 +17,7 @@ const PDFPage: FC = () => {
|
|||||||
const [pageJump, setPageJump] = useState('1');
|
const [pageJump, setPageJump] = useState('1');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
load(`${path}/${entry.name}/${currentPage}.png`);
|
load(`${path}/${entry.name}/${currentPage}.bjpg`);
|
||||||
}, [currentPage]);
|
}, [currentPage]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -77,7 +77,7 @@ const PDFPage: FC = () => {
|
|||||||
</Backdrop>
|
</Backdrop>
|
||||||
) : (
|
) : (
|
||||||
<Box>
|
<Box>
|
||||||
<img src={`data:image/png;base64,${file}`} width="100%" />
|
<img src={`data:image/jpg;base64,${file}`} width="100%" />
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@ -14,11 +14,11 @@
|
|||||||
namespace khofmann
|
namespace khofmann
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read a file and return BASE64 encoded file contents
|
/// Read a file and return file contents
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">Path of File</param>
|
/// <param name="path">Path of BASE64 encoded File</param>
|
||||||
/// <param name="alloc">Allocator</param>
|
/// <param name="alloc">Allocator</param>
|
||||||
/// <returns>BASE64 encoded file contents</returns>
|
/// <returns>File contents</returns>
|
||||||
static rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
|
static rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
|
||||||
{
|
{
|
||||||
FILE* file = fopen(path, "r");
|
FILE* file = fopen(path, "r");
|
||||||
@ -31,7 +31,7 @@ namespace khofmann
|
|||||||
fread(string, fsize, 1, file);
|
fread(string, fsize, 1, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
return rapidjson::Value(macaron::Base64::Encode(string, fsize).c_str(), alloc);
|
return rapidjson::Value(string, alloc);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rapidjson::Value("", alloc);
|
return rapidjson::Value("", alloc);
|
||||||
@ -63,7 +63,7 @@ namespace khofmann
|
|||||||
std::string dirPath(path);
|
std::string dirPath(path);
|
||||||
dirPath += dir->d_name;
|
dirPath += dir->d_name;
|
||||||
std::string thumb(dirPath);
|
std::string thumb(dirPath);
|
||||||
thumb += "/thumb.png";
|
thumb += "/thumb.bjpg";
|
||||||
FILE* check = fopen(thumb.c_str(), "r");
|
FILE* check = fopen(thumb.c_str(), "r");
|
||||||
if (check)
|
if (check)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user