Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alcros33 committed Jan 5, 2020
0 parents commit c56cd3f
Show file tree
Hide file tree
Showing 20 changed files with 1,539 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
86 changes: 86 additions & 0 deletions Card2PDF/CardPDFWriter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from PyQt5.QtCore import QFile, QIODevice, QSizeF, QMarginsF
from PyQt5.QtGui import QPainter, QPdfWriter, QPageSize, QPen

from enum import Enum

def assert_file_open(func):
def wrapper(obj, *args, **kwargs):
if not obj.isOpen():
raise ValueError("PDF file is closed")
return func(obj, *args, **kwargs)
return wrapper

class CardPDFWriter:
RESOLUTION = 300 # 300dpi

@staticmethod
def mm2pix(args):
return [x*(CardPDFWriter.RESOLUTION/25.4) for x in args]

def __init__(self, file_name, card_format, paper_format, separation=[0.8, 0.8]):
self.cardFormat = self.mm2pix(card_format)
self.paperFormat = self.mm2pix(paper_format)
self.separation = self.mm2pix(separation)
self.paperFormatMM = paper_format
self.file = QFile(str(file_name))
self.file.open(QIODevice.WriteOnly)

self.writer = QPdfWriter(self.file)
self.writer.setResolution(self.RESOLUTION)
self.writer.setPageSizeMM(QSizeF(*self.paperFormatMM))
self.writer.setPageMargins(QMarginsF(0, 0, 0, 0))

self.painter = QPainter(self.writer)
self.pen = QPen()
self.pen.setWidth(self.mm2pix([1])[0])
self.painter.setPen(self.pen)

self.bleeding = [0,0]
self.bleeding[0] = (self.paperFormat[0] % int(self.cardFormat[0] + self.separation[0]))/2
self.bleeding[1] = (self.paperFormat[1] % int(self.cardFormat[1] + self.separation[1]))/2
self.cursor = self.bleeding[:]

self._setupPage()

def _setupPage(self):
# self.writer.setPageSizeMM(QSizeF(*self.paperFormatMM))
# self.writer.setPageMargins(QMarginsF(0, 0, 0, 0))
# Horizontal lines
pos = self.bleeding[0] - self.separation[0]/2
while (pos < self.paperFormat[0]):
self.painter.drawLine(pos, 0, pos, self.paperFormat[1])
pos += self.cardFormat[0] + self.separation[0]

# Vertical lines
pos = self.bleeding[1] - self.separation[1]/2
while (pos < self.paperFormat[1]):
self.painter.drawLine(0, pos, self.paperFormat[0], pos)
pos += self.cardFormat[1] + self.separation[1]

@assert_file_open
def addPage(self):
self.writer.newPage()
self._setupPage()

@assert_file_open
def addCard(self, card, num_copies):
for _ in range(num_copies):
if self.cursor[1] > (self.paperFormat[1] - self.bleeding[1] - self.cardFormat[1]):
self.cursor = self.bleeding[:]
self.addPage()

self.painter.drawPixmap(*self.cursor, *self.cardFormat, card)
self.cursor[0] += self.cardFormat[0] + self.separation[0]

if self.cursor[0] > (self.paperFormat[0] - self.bleeding[0] - self.cardFormat[0]):
self.cursor[0] = self.bleeding[0]
self.cursor[1] += self.cardFormat[1] + self.separation[1]


@assert_file_open
def close(self):
self.painter.end()
self.file.flush()
self.file.close()

def isOpen(self): return self.file.isOpen()
Loading

0 comments on commit c56cd3f

Please sign in to comment.