Skip to content

Commit

Permalink
Merge pull request #93 from MathewBiddle/certificate
Browse files Browse the repository at this point in the history
adding a certificate maker
  • Loading branch information
MathewBiddle authored May 17, 2023
2 parents 5535304 + 5f869e9 commit 6038447
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 8,512 deletions.
64 changes: 58 additions & 6 deletions certificate/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
# Certificates for The Carpentries

To create certificates, run:
```bash
python make_certificate.py
```

There are two ways to build certificates from this repo, one depends on the python package cairosvg which in turn depends on cairo development libraries being installed. To use this method, use `bin/certificates.py` to build certificates.
In `make_certificate.py` edit `config` for the persons name and date of the workshop.

The second, pure python method uses the python packages jinja2, jinja2-cli and svglib to build the certificates.
This will generate `.svg` and `.pdf` certificates in the `certs/` directory. File names follow the convention `firstName_lastName_date.[svg|pdf]`

To build certificates this way, you can run:
```
jinja2 swc-attendance.svg -D name="Firstname Lastname" -D date="Nov. 6, 2017" -D instructor="Some Instructor Name" > lastname_firstname.svg
svg2pdf lastname_firstname.svg


## Create the template certificate.svg

### Using Microsoft Power Point
1. Create the certificate of interest in MS Power Point.
1. Make sure to include the jinja template variables in the location you want the content to appear.
1. For example, the appicants first name will replace the variable `{{configs.name.first_name}}` in the svg file.
1. Save the slide as SVG. File -> Save As -> select "Scalable Vector Graphics". Choose to save only the one slide.
1. This is where is gets tricky. The SVG from Power Point has some very specific formatting for text location on the slide. For example, text will be stored as `tspan` with specific x & y starting locations on the page. Unfortunately, this doesn't ensure the content is centered on the page, especially when we have variable content through the jinja template.
```xml
<tspan font-family="Garamond,Garamond_MSFontService,sans-serif" font-stretch="normal" font-size="24" x="206.147" y="96">
This certificate is awarded to
</tspan>
```
Instead, we can generalize this to a `text` element and specify center and the y location:
```xml
<text font-family="Garamond,Garamond_MSFontService,sans-serif" font-stretch="normal" font-size="24" dominant-baseline="middle" text-anchor="middle" x="50%" y="270">
This certificate is awarded to
</text>
```
1. Edit the XML in the svg file to adjust the text elements to be percentages in the x-direction. Make any other custom edits to the svg file. Note that Power Point might separate each work into it's own `tspan` element. These can be simplified into one `text` element.
1. Save the template svg as `certificate.svg` in the `templates\` directory.

### Using diagrams.net
1. Using https://app.diagrams.net/ design the certificate.
1. Include the jinja template variables where the information should be replaced.
1. The appicants first name will replace the variable `{{configs.name.first_name}}` in the svg file.
1. Save the image as `svg`
1. File > Export as > SVG...
2. Set the size to `Diagram`
3. Check `Include a copy of my diagram`
4. Check `Embed Images`
5. Leave `Text Settings` as `Default`
6. Leave `Links` as `Automatic`
7. Select `Export` and save to `certificate/bin/templates/certificate.svg`

`make_certificate.py` will pick up the template and insert the appropriate information from the `config` variable.

The variables in the svg should follow the structure of the `config` list:
```python
config = [
{
"name": {"first_name": "Mathew", "last_name": "Biddle"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Abby", "last_name": "Benson"},
"date": "2023-05-19",
},
]
```

first name is accessed through `config.name.first_name`, last name through `config.name.last_name` and date is accessed through `config.date`.
196 changes: 0 additions & 196 deletions certificate/bin/certificates.py

This file was deleted.

102 changes: 102 additions & 0 deletions certificate/bin/make_certificate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
from jinja2 import Environment, FileSystemLoader
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF


def svg2pdf(fname):
drawing = svg2rlg(fname)
outname = fname.replace(".svg", ".pdf")
renderPDF.drawToFile(drawing, outname)


def write_svg_cert(template, config):
root = os.path.dirname(os.path.abspath(__file__))
# root = path to output directory
fname = (
config["name"]["first_name"]
+ "_"
+ config["name"]["last_name"]
+ "_"
+ config["date"]
+ ".svg"
)
filename = os.path.join(root, "certs", fname)
with open(filename, "w") as fh:
fh.write(template.render(configs=config))

svg2pdf(filename)


def load_template():
root = os.path.dirname(os.path.abspath(__file__))
templates_dir = os.path.join(root, "templates")
env = Environment(loader=FileSystemLoader(templates_dir))
template = env.get_template("certificate.svg")
return template


def write_templates(config):
# filename = parse_args()
template = load_template()
write_svg_cert(template, config)


def main(config):
for person in config:
write_templates(person)


if __name__ == "__main__":
config = [
{
"name": {"first_name": "Abby", "last_name": "Benson"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Matt", "last_name": "Biddle"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Julieta", "last_name": "Maldonado"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Errol", "last_name": "Ronje"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Kylie", "last_name": "Langlois"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Laura", "last_name": "Teed"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Kourtney", "last_name": "Burger"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Javier", "last_name": ""},
"date": "2023-05-19",
},
{
"name": {"first_name": "Fernando García", "last_name": "González"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Marine", "last_name": "Lebrec"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Marc", "last_name": "Ghergariu"},
"date": "2023-05-19",
},
{
"name": {"first_name": "Rodrigo J.", "last_name": "Gonçalves"},
"date": "2023-05-19",
},
]

main(config)
22 changes: 0 additions & 22 deletions certificate/bin/makecert.sh

This file was deleted.

Loading

0 comments on commit 6038447

Please sign in to comment.