mirror of
https://github.com/Llewellynvdm/pdflayers.git
synced 2024-12-18 06:54:07 +00:00
Merge pull request #8 from SimonSegerblomRex/new-structure
Restructure single script into package
This commit is contained in:
commit
e40d133d4b
19
README.md
19
README.md
@ -1,19 +1,30 @@
|
|||||||
pdflayers
|
pdflayers
|
||||||
=========
|
=========
|
||||||
|
|
||||||
**pdflayers** is a tool for changing default visibility of PDF layers (OCGS, Optional Content Groups).
|
**pdflayers** is a tool for changing default visibility of PDF layers
|
||||||
|
(OCGs, Optional Content Groups).
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Currently **pdflayers** just consists of a simple python script, so you manually have to install the dependencies:
|
|
||||||
```shell
|
```shell
|
||||||
python3 -m pip install --user pikepdf
|
python3 -m pip install --user https://github.com/SimonSegerblomRex/pdflayers
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
```
|
```
|
||||||
python3 pdflayers.py <input file> <output file> --show <name of layer to be visible in putput>
|
pdflayers <input file> <output file> --show <layer names>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```
|
||||||
|
python -m pdflayers <input file> <output file> --show <layer names>
|
||||||
|
```
|
||||||
|
|
||||||
|
Acknowledgements
|
||||||
|
----------------
|
||||||
|
|
||||||
|
[pikepdf](https://github.com/pikepdf/pikepdf) is doing most of the work.
|
||||||
|
@ -12,44 +12,12 @@ References:
|
|||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
import sys
|
|
||||||
|
|
||||||
import pikepdf
|
import pikepdf
|
||||||
|
|
||||||
|
from pdflayers import utils
|
||||||
|
|
||||||
def set_layer_visibility(pdf, layers_to_show):
|
logger = logging.getLogger(__name__)
|
||||||
"""Set visibility of layers."""
|
|
||||||
try:
|
|
||||||
ocgs = pdf.root.OCProperties.OCGs
|
|
||||||
except (AttributeError, KeyError):
|
|
||||||
logging.error("Unable to locate layers in PDF.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
ocgs_on = []
|
|
||||||
for ocg in ocgs:
|
|
||||||
if ocg.Name in layers_to_show:
|
|
||||||
logging.info("Layer %s will be visible.", ocg.Name)
|
|
||||||
ocgs_on.append(ocg)
|
|
||||||
else:
|
|
||||||
logging.info("Layer %s will be hidden.", ocg.Name)
|
|
||||||
|
|
||||||
ocgs_config = pikepdf.Dictionary(
|
|
||||||
BaseState=pikepdf.Name('/OFF'),
|
|
||||||
ON=ocgs_on,
|
|
||||||
Order=ocgs,
|
|
||||||
)
|
|
||||||
|
|
||||||
pdf.root.OCProperties = pikepdf.Dictionary(
|
|
||||||
D=ocgs_config,
|
|
||||||
OCGs=ocgs,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Needed for google-chrome (at least):
|
|
||||||
for ocg in ocgs:
|
|
||||||
if '/View' in ocg.Usage:
|
|
||||||
del ocg.Usage.View
|
|
||||||
if '/Print' in ocg.Usage:
|
|
||||||
del ocg.Usage.Print
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -72,7 +40,7 @@ def main():
|
|||||||
|
|
||||||
pdf = pikepdf.open(args.input, password=args.password)
|
pdf = pikepdf.open(args.input, password=args.password)
|
||||||
|
|
||||||
set_layer_visibility(pdf, args.show)
|
utils.set_layer_visibility(pdf, args.show)
|
||||||
pdf.remove_unreferenced_resources()
|
pdf.remove_unreferenced_resources()
|
||||||
|
|
||||||
save_options = {
|
save_options = {
|
42
pdflayers/utils.py
Normal file
42
pdflayers/utils.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
"""Utility functions used in pdflayers."""
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pikepdf
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def set_layer_visibility(pdf, layers_to_show):
|
||||||
|
"""Set visibility of layers."""
|
||||||
|
try:
|
||||||
|
ocgs = pdf.root.OCProperties.OCGs
|
||||||
|
except (AttributeError, KeyError):
|
||||||
|
logger.error("Unable to locate layers in PDF.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
ocgs_on = []
|
||||||
|
for ocg in ocgs:
|
||||||
|
if ocg.Name in layers_to_show:
|
||||||
|
logger.info("Layer %s will be visible.", ocg.Name)
|
||||||
|
ocgs_on.append(ocg)
|
||||||
|
else:
|
||||||
|
logger.info("Layer %s will be hidden.", ocg.Name)
|
||||||
|
|
||||||
|
ocgs_config = pikepdf.Dictionary(
|
||||||
|
BaseState=pikepdf.Name('/OFF'),
|
||||||
|
ON=ocgs_on,
|
||||||
|
Order=ocgs,
|
||||||
|
)
|
||||||
|
|
||||||
|
pdf.root.OCProperties = pikepdf.Dictionary(
|
||||||
|
D=ocgs_config,
|
||||||
|
OCGs=ocgs,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Needed for google-chrome (at least):
|
||||||
|
for ocg in ocgs:
|
||||||
|
if '/View' in ocg.Usage:
|
||||||
|
del ocg.Usage.View
|
||||||
|
if '/Print' in ocg.Usage:
|
||||||
|
del ocg.Usage.Print
|
33
setup.py
Normal file
33
setup.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Setup."""
|
||||||
|
import setuptools
|
||||||
|
|
||||||
|
|
||||||
|
def _readme():
|
||||||
|
with open('README.md', 'r') as file:
|
||||||
|
return file.read()
|
||||||
|
|
||||||
|
|
||||||
|
setuptools.setup(
|
||||||
|
name='pdflayers',
|
||||||
|
version='0.1.0',
|
||||||
|
author='Simon Segerblom Rex',
|
||||||
|
description='PDF layer handling.',
|
||||||
|
long_description=_readme(),
|
||||||
|
long_description_content_type='text/markdown',
|
||||||
|
url='https://github.com/SimonSegerblomRex/pdflayers',
|
||||||
|
license='MIT',
|
||||||
|
packages=setuptools.find_packages(),
|
||||||
|
entry_points={
|
||||||
|
'console_scripts': [
|
||||||
|
'pdflayers = pdflayers.__main__:main',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
python_requires='>=3.5',
|
||||||
|
install_requires=[
|
||||||
|
'pikepdf',
|
||||||
|
],
|
||||||
|
classifiers=[
|
||||||
|
'Python :: 3'
|
||||||
|
],
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user