Skip to content

Instantly share code, notes, and snippets.

@bschne
Created October 14, 2024 13:09
Show Gist options
  • Select an option

  • Save bschne/00604c47016408e38d529b03881e44f5 to your computer and use it in GitHub Desktop.

Select an option

Save bschne/00604c47016408e38d529b03881e44f5 to your computer and use it in GitHub Desktop.
PDF to Clipboard Text
# Extracts text from a PDF file and copies it to clipboard
# This relies on pdftotext, which you can install via "brew install poppler"
# pbcopy is available by default on macOS, but if you're on linux, this won't work
ptxt() {
if [[ -z "$1" ]]; then
echo "Usage: ptxt <path_to_pdf>"
return 1
fi
local pdf_path="$1"
# Check if the file exists
if [[ ! -f "$pdf_path" ]]; then
echo "Error: File '$pdf_path' not found."
return 1
fi
# Convert PDF to text and copy to clipboard
pdftotext "$pdf_path" - | pbcopy
if [[ $? -eq 0 ]]; then
echo "Text from '$pdf_path' has been copied to the clipboard."
else
echo "Failed to convert '$pdf_path' to text."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment