Last active
August 1, 2016 14:10
-
-
Save altbdoor/008fbaeb8932169b6530d2fd6c4197b0 to your computer and use it in GitHub Desktop.
Scripts for combining PDF receipts into one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Reflection; | |
/* | |
Compiled with | |
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:gm_pdf.exe gm_pdf.cs | |
*/ | |
public class GM_PDF | |
{ | |
public static void Main() | |
{ | |
string CurrentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); | |
string[] FullPdfFiles = Directory.GetFiles(CurrentPath, "*.pdf", SearchOption.TopDirectoryOnly); | |
List<string> FileNames = new List<string>(); | |
foreach (string FilePath in FullPdfFiles) | |
{ | |
string FileName = Path.GetFileName(FilePath); | |
FileName = FileName.Replace(".pdf", ""); | |
if (FileName != "output_gm_compiled") | |
{ | |
FileNames.Add(FileName); | |
} | |
} | |
int FilesLength = FileNames.Count; | |
Console.WriteLine("Found " + FilesLength + " items in current directory."); | |
Console.WriteLine("Processing PDF to PNG..."); | |
foreach (string FileName in FileNames) | |
{ | |
Console.WriteLine(" > Processing " + FileName + ".pdf"); | |
ExecCli("gm convert -density 200 \"" + FileName + ".pdf\" -quality 100 -crop 1460x940+95+285 -colorspace Gray -bordercolor white -border 120x200 \"output_gm_" + FileName + ".png\""); | |
} | |
Console.WriteLine("Combining PNG..."); | |
List<string> PngFileNames = new List<string>(FileNames); | |
for (int i=0; i<FilesLength; i+=2) | |
{ | |
int NextIndex = i + 1; | |
if (NextIndex < FilesLength) | |
{ | |
string CurrentFile = "output_gm_" + FileNames[i] + ".png"; | |
string NextFile = "output_gm_" + FileNames[NextIndex] + ".png"; | |
Console.WriteLine(" > Combining " + CurrentFile + " and " + NextFile); | |
ExecCli("gm convert \"" + CurrentFile + "\" \"" + NextFile + "\" -gravity South -chop 0x200 -append \"" + CurrentFile + "\""); | |
PngFileNames.Remove(FileNames[NextIndex]); | |
File.Delete(NextFile); | |
} | |
} | |
Console.WriteLine("Combining PDF..."); | |
string FinalArg = "gm convert"; | |
foreach (string FileName in PngFileNames) | |
{ | |
FinalArg += " \"output_gm_" + FileName + ".png\" "; | |
} | |
FinalArg += " -compress jpeg -resize 1140x -extent 1240x1753-50 -gravity center -units PixelsPerInch -density 150x150 -repage 1240x1753 output_gm_compiled.pdf"; | |
ExecCli(FinalArg); | |
Console.WriteLine("Cleaning up..."); | |
foreach (string FileName in PngFileNames) | |
{ | |
File.Delete("output_gm_" + FileName + ".png"); | |
} | |
Console.WriteLine("Done"); | |
Console.ReadLine(); | |
} | |
private static void ExecCli (string CommandString) | |
{ | |
Process Proc = new Process(); | |
ProcessStartInfo ProcInfo = new ProcessStartInfo(); | |
ProcInfo.WindowStyle = ProcessWindowStyle.Hidden; | |
ProcInfo.FileName = "cmd.exe"; | |
ProcInfo.Arguments = "/C " + CommandString; | |
Proc.StartInfo = ProcInfo; | |
Proc.Start(); | |
Proc.WaitForExit(); | |
if (Proc.ExitCode != 0) | |
{ | |
Console.WriteLine("\n<!> Something happened.\n The process has stopped unexpectedly from a command line execution."); | |
Console.ReadLine(); | |
Environment.Exit(1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
files = [] | |
for file in os.listdir('.'): | |
if os.path.isfile(file) and file.endswith('.pdf'): | |
files.append(file.replace('.pdf', '')) | |
files.remove('output_gm_compiled') | |
files_length = len(files) | |
print 'Processing PDF to PNG...' | |
for idx, file in enumerate(files): | |
print '> {}.pdf, {}/{}'.format(file, idx + 1, files_length) | |
os.system('gm convert -density 200 {}.pdf -quality 100 -crop 1460x940+95+285 -colorspace Gray -bordercolor white -border 120x200 output_gm_{}.png'.format( | |
file, file | |
)) | |
print 'Combining PNG...' | |
png_files = list(files) | |
for current in range(0, files_length, 2): | |
next = current + 1 | |
if next < files_length: | |
current_file = 'output_gm_{}.png'.format(files[current]) | |
next_file = 'output_gm_{}.png'.format(files[next]) | |
print '> {} and {}'.format(current_file, next_file) | |
os.system('gm convert {} {} -gravity South -chop 0x200 -append {}'.format( | |
current_file, next_file, current_file | |
)) | |
png_files.remove(files[next]) | |
os.remove(next_file) | |
print 'Combining PDF...' | |
gm_args = 'gm convert' | |
for file in png_files: | |
gm_args += ' output_gm_{}.png '.format(file) | |
gm_args += ' -compress jpeg -resize 1140x -extent 1240x1753-50 -gravity center -units PixelsPerInch -density 150x150 -repage 1240x1753 output_gm_compiled.pdf' | |
os.system(gm_args) | |
print 'Cleaning up...' | |
for file in png_files: | |
os.remove('output_gm_{}.png'.format(file)) | |
print 'Done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment