Skip to content

Instantly share code, notes, and snippets.

@bkildow
Created April 4, 2023 14:01
Show Gist options
  • Save bkildow/6474ec359b7761164872227e4c72b30a to your computer and use it in GitHub Desktop.
Save bkildow/6474ec359b7761164872227e4c72b30a to your computer and use it in GitHub Desktop.
A11y enhancements with PikePDF (Python)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Open the PDF for editing."
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"import pikepdf\n",
"\n",
"pdf = pikepdf.open('input/orig.pdf')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Experimenting with merging and copying StructTreeRoot. While the merging work, the StructTreeRoot stuff didn't work quite right."
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"# cover_pdf = pikepdf.open('input/one.pdf')\n",
"# pdf = pikepdf.open('input/two.pdf')\n",
"\n",
"# Save the structTreeRoot from the full PDF to reuse later\n",
"# structTree = pdf.Root.StructTreeRoot\n",
"\n",
"# Make pdf1's cover page become pdf2's cover page\n",
"# pdf.pages[0] = cover_pdf.pages[0]\n",
"# pdf.Root.StructTreeRoot = structTree"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Adds a metadata title and view preferences which fixes this: https://helpx.adobe.com/acrobat/using/create-verify-pdf-accessibility.html?trackingid=KACNN#DocTitle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Add a metadata title.\n",
"with pdf.open_metadata() as meta:\n",
" meta['dc:title'] = \"Title example\"\n",
"\n",
"# Create a new ViewerPreferences dictionary with /DisplayDocTitle set to true\n",
"viewer_prefs = pikepdf.Dictionary(DisplayDocTitle=True)\n",
"\n",
"# Assign the new ViewerPreferences dictionary to the document root\n",
"pdf.Root.ViewerPreferences = viewer_prefs"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of adding the needed Lang to the PDF. In this example we are adding French. Fixes this: https://helpx.adobe.com/acrobat/using/create-verify-pdf-accessibility.html?trackingid=KACNN#PrimeLang"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"pdf.Root.Lang = pikepdf.String(\"fr\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Adds `\"/Tabs\": \"/S\",` to every page which is needed to pass this check: https://helpx.adobe.com/acrobat/using/create-verify-pdf-accessibility.html?trackingid=KACNN#TabOrder. Sets the tab order to the structure of the page."
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"for page in pdf.pages:\n",
" page.Tabs = pikepdf.Name('/S')\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"pdf.save('output/test.pdf')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pdf-kxww3sBo",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment