Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Last active May 20, 2024 07:03
Show Gist options
  • Save kidGodzilla/9d500630b77d12880a4834ac9d94d886 to your computer and use it in GitHub Desktop.
Save kidGodzilla/9d500630b77d12880a4834ac9d94d886 to your computer and use it in GitHub Desktop.
PDF to PNG Script for Quickdrop App
# PDF to PNG shell script for Quickdrop app
# https://quickdrop.antran.app/
# pdfburst shell script
# chmod 0755 pdfburst
#!/usr/bin/swift
import Foundation
import PDFKit
func splitPDF(inputPath: String) {
let docURL = URL(fileURLWithPath: inputPath)
guard let pdfDocument = PDFDocument(url: docURL) else {
print("Error: Unable to open PDF at \(inputPath)")
return
}
guard pdfDocument.pageCount > 1 else {
print(inputPath)
return
}
let baseFileName = docURL.deletingPathExtension()
for i in 0..<pdfDocument.pageCount {
guard let page = pdfDocument.page(at: i) else { continue }
let newDocument = PDFDocument()
newDocument.insert(page, at: 0)
let outputPath = baseFileName.path(percentEncoded: false) + "_page_\(i+1).pdf"
newDocument.write(to: URL(fileURLWithPath: outputPath))
print(outputPath)
}
}
if CommandLine.arguments.count < 2 {
print("Usage: \(CommandLine.arguments.first!) <inputPDF>")
exit(1)
}
let inputPath = CommandLine.arguments[1]
splitPDF(inputPath: inputPath)
# PDF to PNG Shell Script
/usr/local/bin/pdfburst "{{inputFilePath}}" | awk '{print "/usr/bin/sips -s format png \"" $NF "\" -z 2112 1632 --out \"" $0 ".png\"; rm -f \"" $NF "\""}' | bash
@kidGodzilla
Copy link
Author

  1. Create a shell script called pdfburst (this splits a PDF into intermediate pages)
  2. Place in /usr/local/bin/ and chmod 0755
  3. Add the custom script to the Quickdrop app

@kidGodzilla
Copy link
Author

Note: Assumes 8.5"x11" pages

@antranapp
Copy link

This is awesome @kidGodzilla. It's even more awesome that it is written in Swift 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment