Files
@ c6012ca02927
Branch filter:
Location: pcgen-xml2pdf/pcgen-xml2pdf.py
c6012ca02927
14.4 KiB
text/x-python
Add Skills
History/Insight broken in ALT sheet (duplicate field name)
History/Insight broken in ALT sheet (duplicate field name)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | #!/usr/bin/env python3
from pathlib import Path
from pikepdf import Pdf, Name, String, Array
import io, hashlib, hmac
import json
import sys
import xml.etree.ElementTree as ET
def get_fields(pdf):
"""
Extract info about the interactive fields present in the AcroForm of the
pikepdf.PdfObject passed.
Also, if the PDF has attr: NeedAppearances, ensures that it's value is True.
param: pdf - instance of<pikepdf.PdfObject>
returns: None, if the Pdf doesn't have an AcroForm, or a nested dict containing
the fields names as the keys, with their value being a dict following this specification:
'/FT' : field type;
'/V' : current value present in the field;
'/DV': if the field is one of the text types ('/Tx' or '/Ch'),
this shows what value is the default one when the field
has not yet been filled;
'options': if the field is one of the button types ('/Btn' or '/RBtn'),
this holds a list that contains the values used to represent when the
button is `off` or `on`, or `off` and all the values used to represent
the different options, in case it's a '/RBtn'. This method tries to
ensure that options[0] will always be the `off` value, but this can't be
guaranteed to always be true, since some Pdf-Form-Maker softwares might
not follow all the Pdf 1.7 ISO specs.
extra_info: some common types of fields:
'/Tx': Text Field;
'/Ch': Options List or Combo Box;
'/Btn': Interactive Button;
'/RBtn': Radio Button Parent;
'/Sig': Signature Field (not suported)
"""
if not hasattr(pdf.Root, "AcroForm"):
return None
if hasattr(pdf.Root.AcroForm, "NeedAppearances"):
# If there's NeedAppearances in the Pdf AcroForm,
# ensure that it's value is True
pdf.Root.AcroForm.NeedAppearances = True
fields = {}
radio_repeats = []
off_values = (
"/Off",
"/No",
"/0",
"/Null",
"/False",
"/Nill",
)
# These are some `off` values that I've seen in some Pdf Forms
for i in range(len(pdf.pages)):
for annot in pdf.pages[i].Annots:
if not hasattr(annot, "FT"): # This handles kids of radio buttons
parent_str = str(annot.Parent.T)
if parent_str in radio_repeats:
# check if annot.Parent isn't yet in the repeats list.
# if it is, just get the '/Yes' value
cur_list = fields[parent_str]["options"]
off_value = cur_list[0]
options = [item for item in annot.AP.N if item != off_value]
cur_list.extend(options)
fields[parent_str]["options"] = cur_list
continue
# options = [item for item in annot.AP.N]
# access the kids keys of '/N' to get the
# '/Off' and '/Yes' values of this first
# radio button kid
# if options[0] not in off_values:
# options.reverse()
# info_field = {
# "/FT": "/RBtn", # This '/RBtn' notation is not standartized
# "/V": str(annot.AS),
# "options": options,
# }
# fields.update({str(annot.Parent.T): info_field})
# radio_repeats.append(parent_str)
else:
if annot.FT == "/Btn":
if hasattr(annot, "A"):
# most likely JS Code utility button.
# They're not truly interactive, so skip
continue
# Else, must be a normal checkbox.
# It's way simpler to work with them
off_on_values = [item for item in annot.AP.N]
if off_on_values[0] not in off_values:
# Trying to ensure that index[0] will
# always be the '/Off' value
off_on_values.reverse()
info_field = {
"/FT": "/Btn",
"/V": str(annot.AS),
"options": off_on_values,
}
elif annot.FT == '/Sig':
# Placeholder for signatures fields
continue
else:
# Otherwise, must be an text field
# ('/Tx' or '/Ch')
info_field = {
"/FT": str(annot.FT),
# "/V": str(annot.V),
"/DV": str(annot.DV),
}
fields.update({str(annot.T): info_field})
return fields
def update_fields(pdf, fields):
"""
Update Pdf interactive form fields values
param: pdf - instance of<pikepdf.PdfObject> to update fields values
param: fields - dict with fields names and their desired value
For better performance, it's recommended to send only the fields names
that need their values updated, as this method does nested iterations.
The desired values sent are transformed into strings before being updated.
You can send values that are not originally in fields of the type '/Ch'.
They'll be added to these interactive fields, and their value switched to
the desired value.
warning: this method doesn't try to interpret booleans as `off`/`on` values
of buttons. Instead, make the values of buttons fields be one of those listed in
the 'options' list when using get_fields(). Otherwise, the field might not work as expected
"""
for i in range(len(pdf.pages)):
for annot in pdf.pages[i].Annots:
for field, item in fields.items():
value = str(item)
if not hasattr(annot, "FT"):
if str(annot.Parent.T) != field:
continue
else:
foo = Name(value)
annot.Parent.AS = foo
annot.AS = foo
# The Parent holds in it's .AS the `on` value of the kid
# that's `on`, but the kid also needs tho have it's .AS
# set to the `on` value to show itself the right way
continue
elif field != str(annot.T):
continue
field_type = str(annot.FT)
if field_type == "/Tx":
foo = String(value)
annot.V = foo
annot.DV = foo
# DV's are also updated because this "forces"
# most pdfviewers to display them
elif field_type == "/Btn":
if hasattr(annot, "A"):
# This skips most JS Code buttons
continue
foo = String(value)
annot.AS = Name(value)
annot.V = foo
annot.DV = foo
# Normal checkboxes (usually) hold their values
# both in the AS and V
elif field_type == "/Ch":
opt_list = [str(item) for item in annot.Opt]
if value not in opt_list:
# Add the value to the original '/Opt' -- why
# limit yourself to only the values that the pdf form
# creator wants you to use?
opt_list.append(value)
opt_list.sort()
annot.Opt = Array(opt_list)
foo = String(value)
annot.V = foo
annot.DV = foo
elif field_type == "/Sig":
# Placeholder for signature fields. Needs more research
continue
if __name__ == '__main__':
# Get the script's directory
script_directory = str(Path(__file__).parent.absolute())
xmlpath = sys.argv[1]
#outpath = xmlpath+".pdf"
outpath = "/tmp/"+str(Path(xmlpath).name)+".pdf"
root = ET.parse(xmlpath)
charactertype = root.find(".//basics/charactertype").text
print("charactertype: "+charactertype)
if charactertype == "PC":
source = script_directory + "/templates/DnD_5E_CharacterSheet - Form Fillable.pdf"
elif charactertype == "NPC":
source = script_directory + "/templates/Character Sheet - Alternative - Form Fillable.pdf"
else:
print("Error: Unknown charactertype")
exit(1)
with open(source, "rb") as f:
digest = hashlib.file_digest(f, "sha1")
hash = digest.hexdigest()
print("source: '"+source+"' (SHA1="+hash+")")
if hash == "e58f7571d0da0d7bdb552301a3cfec17d0abca37":
pdftype = "ORG"
elif hash == "074f7998dff511d6626febdcf6cd48c635184d76":
pdftype = "ALT"
else:
print("Error: Unknown SHA1:"+hash)
exit(1)
new_pdf = Pdf.open(source)
fields = get_fields(new_pdf)
print(json.dumps(fields, indent = 4))
new_fields = {
# "City Text Box": "Bababooie",
# "Country Combo Box": "NoWhere",
# "Gender List Box": "Helicopter",
# "Height Formatted Field": "210",
# "Driving License Check Box": "/Yes",
### BIO ###
"ClassLevel": root.find(".//shortform").text,
"Background": root.find(".//special_quality[type='BACKGROUND.BACKGROUND SELECTION.SPECIALQUALITY']/name").text,
"PlayerName": root.find(".//basics/playername").text,
"CharacterName": root.find(".//basics/name").text,
"Race ": root.find(".//basics/race[1]").text,
"Alignment": root.find(".//basics/alignment/long").text,
"XP": root.find(".//basics/experience/current").text,
### ABILITIES ###
"STR": root.find(".//ability/name[short='STR']/../score").text,
"STRmod": root.find(".//ability/name[short='STR']/../modifier").text,
"DEX": root.find(".//ability/name[short='DEX']/../score").text,
"DEXmod ": root.find(".//ability/name[short='DEX']/../modifier").text,
"CON": root.find(".//ability/name[short='CON']/../score").text,
"CONmod": root.find(".//ability/name[short='CON']/../modifier").text,
"INT": root.find(".//ability/name[short='INT']/../score").text,
"INTmod": root.find(".//ability/name[short='INT']/../modifier").text,
"WIS": root.find(".//ability/name[short='WIS']/../score").text,
"WISmod": root.find(".//ability/name[short='WIS']/../modifier").text,
"CHA": root.find(".//ability/name[short='CHA']/../score").text,
"CHamod": root.find(".//ability/name[short='CHA']/../modifier").text,
### HIT POINTS ###
"HPMax": root.find(".//points").text,
### SAVING THROWS ###
# original sheet only #
"ST Strength": root.find(".//saving_throw/name[long='strength']/../total").text if pdftype == "ORG" else "/False",
"ST Dexterity": root.find(".//saving_throw/name[long='dexterity']/../total").text if pdftype == "ORG" else "/False",
"ST Constitution": root.find(".//saving_throw/name[long='constitution']/../total").text if pdftype == "ORG" else "/False",
"ST Intelligence": root.find(".//saving_throw/name[long='intelligence']/../total").text if pdftype == "ORG" else "/False",
"ST Wisdom": root.find(".//saving_throw/name[long='wisdom']/../total").text if pdftype == "ORG" else "/False",
"ST Charisma": root.find(".//saving_throw/name[long='charisma']/../total").text if pdftype == "ORG" else "/False",
# alternative sheet #
"SavingThrows": root.find(".//saving_throw/name[long='strength']/../total").text,
"SavingThrows2": root.find(".//saving_throw/name[long='dexterity']/../total").text,
"SavingThrows3": root.find(".//saving_throw/name[long='constitution']/../total").text,
"SavingThrows4": root.find(".//saving_throw/name[long='intelligence']/../total").text,
"SavingThrows5": root.find(".//saving_throw/name[long='wisdom']/../total").text,
"SavingThrows6": root.find(".//saving_throw/name[long='charisma']/../total").text,
### COMBAT ###
"ProfBonus": root.find(".//proficiency_bonus").text,
"AC": root.find(".//armor_class/total").text,
"Initiative": root.find(".//initiative/total").text,
"Speed": int(root.find(".//move/move[name='Walk']/squares").text) * 5,
### SKILLS ###
"Acrobatics": root.find(".//skill[name='Acrobatics']/skill_mod").text,
"Animal": root.find(".//skill[name='Animal Handling']/skill_mod").text,
"Animal Handling": root.find(".//skill[name='Animal Handling']/skill_mod").text,
"Arcana": root.find(".//skill[name='Arcana']/skill_mod").text,
"Athletics": root.find(".//skill[name='Athletics']/skill_mod").text,
"Deception": root.find(".//skill[name='Deception']/skill_mod").text,
"Deception ": root.find(".//skill[name='Deception']/skill_mod").text,
#"History": root.find(".//skill[name='History']/skill_mod").text,
"History ": root.find(".//skill[name='History']/skill_mod").text,
"Insight": root.find(".//skill[name='Insight']/skill_mod").text,
"Intimidation": root.find(".//skill[name='Intimidation']/skill_mod").text,
"Investigation": root.find(".//skill[name='Investigation']/skill_mod").text,
"Investigation ": root.find(".//skill[name='Investigation']/skill_mod").text,
"Medicine": root.find(".//skill[name='Medicine']/skill_mod").text,
"Nature": root.find(".//skill[name='Nature']/skill_mod").text,
"Perception": root.find(".//skill[name='Perception']/skill_mod").text,
"Perception ": root.find(".//skill[name='Perception']/skill_mod").text,
"Performance": root.find(".//skill[name='Performance']/skill_mod").text,
"Persuasion": root.find(".//skill[name='Deception']/skill_mod").text,
"Religion": root.find(".//skill[name='Religion']/skill_mod").text,
"SleightofHand": root.find(".//skill[name='Sleight of Hand']/skill_mod").text,
"Stealth": root.find(".//skill[name='Stealth']/skill_mod").text,
"Stealth ": root.find(".//skill[name='Stealth']/skill_mod").text,
"Survival": root.find(".//skill[name='Survival']/skill_mod").text,
}
update_fields(new_pdf, new_fields)
new_pdf.save(outpath)
|