# Add PDFDocEncoding to standard codecs

**URL:** <https://discuss.python.org/t/add-pdfdocencoding-to-standard-codecs/15214>\
**Category:** Ideas\
**Created:** [April 21, 2022, 9:21am UTC](https://discuss.python.org/t/add-pdfdocencoding-to-standard-codecs/15214 "2022-04-21T09:21:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johns1c](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/johns1c/32/6945_2.png) [@johns1c](https://discuss.python.org/u/johns1c)\
**Post date:** [April 21, 2022, 9:21am UTC](https://discuss.python.org/t/add-pdfdocencoding-to-standard-codecs/15214/1 "2022-04-21T09:21:20Z")

</div>

Python is commonly used to manipulate PDF files and the file format has been for some time an open standard. Packages such as PyPDF2, PDFMiner and user written python code have taken different approaches to parsing these files and dealing with the encodings used. Although the Codecs package can be extended this is not trivial and has performance hits compared with using a built in codec so in practice many use Latin-1 which is slightly different to PDFDocEncoding i.e is **wrong**.

Python advertises it is “batteries included” and has built in support for other widely used formats. This change would go some way to supporting those processing PDFs using Python and would allow a canonical solution to be used across packages and code “one method to rule them all”.

There are already examples of supporting PDF artifacts in the standard library. For example, the **base64** module supports ASCI85 encoded strings, optionally surrounded by the `<~` and `~\>’ tags since Python 3.4.

Such a change should be implemented in a way that is performant and compliant with the specification. This would be a major advantage compared to the current approaches.

---

<div class="post-metadata">

**Author:** ![malemburg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/malemburg/32/50_2.png) [@malemburg](https://discuss.python.org/u/malemburg)\
**Post date:** [April 21, 2022, 10:37am UTC](https://discuss.python.org/t/add-pdfdocencoding-to-standard-codecs/15214/2 "2022-04-21T10:37:44Z")

</div>

We’re generally hesitant to add new encodings to Python, but this appears to be a case where the basic criteria are fulfilled:

- in current use
- in wide-spread use
- won’t get replaced by e.g. UTF-8 anytime soon

I’ve done some research and found these resources:

- [PDF Reference 1.7](https://web.archive.org/web/20090303210950/https://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf) (from [archive.org](http://archive.org), since the original Adobe link doesn’t work anymore)  
This includes the encoding map in section D.2
- Example implementation available as part of pikepdf: [pikepdf/codec.py at master · pikepdf/pikepdf · GitHub](https://github.com/pikepdf/pikepdf/blob/master/src/pikepdf/codec.py)  
However, this implementation is custom one. It would be better to implement the codec as standard charmap codec (see e.g. [cpython/cp1252.py at main · python/cpython · GitHub](https://github.com/python/cpython/blob/main/Lib/encodings/cp1252.py) as reference).

I don’t think a PEP is needed for new codecs. Even though it comes as a new stdlib module (which would require a PEP), the codec will only be used via the codec interface. Opening a ticket and providing a PR with the implementation should be enough.

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [April 21, 2022, 11:44am UTC](https://discuss.python.org/t/add-pdfdocencoding-to-standard-codecs/15214/3 "2022-04-21T11:44:34Z")

</div>

The Pikepdf encoding documentation also seems somewhat useful (if somewhat lacking in specifics):

[https://pikepdf.readthedocs.io/en/latest/topics/encoding.html](https://pikepdf.readthedocs.io/en/latest/topics/encoding.html)
