Skip to content

Instantly share code, notes, and snippets.

@csthompson
Last active April 25, 2021 19:09
Show Gist options
  • Save csthompson/cc9f62bd600d89e5d699e056678aa400 to your computer and use it in GitHub Desktop.
Save csthompson/cc9f62bd600d89e5d699e056678aa400 to your computer and use it in GitHub Desktop.
func main() {
// Open the cards file
inputFile, err := os.Open("cards.json")
if err != nil {
fmt.Println(err)
}
defer inputFile.Close()
// Read the cards file into bytes
byteValue, _ := ioutil.ReadAll(inputFile)
// Initialize the slice of cards that will be printed
var cards []Card
// Unmarshal the input file into a slice of Card objects
json.Unmarshal(byteValue, &cards)
// Read the card template file
b, err := ioutil.ReadFile("card.html")
if err != nil {
fmt.Print(err)
}
// Convert the template to a string
tmpl := string(b)
// Initialize a new liquid templating engine
engine := liquid.NewEngine()
// Get the current working directory
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
// The name of the output file to screenshot in Chrome
renderFile := fmt.Sprintf(`file://%s/render.html`, path)
// Iterate over the slice of cards
for i, c := range cards {
// Create a binding for the card object
bindings := map[string]interface{}{
"card": c,
}
// Inject the bindings to the liquid engine and use the template from the card.html file
out, err := engine.ParseAndRenderString(tmpl, bindings)
if err != nil {
log.Fatalln(err)
}
// Convert the result from the template to bytes so that it can be written to a file
outBytes := []byte(out)
// Create a filename for the screenshot
fname := fmt.Sprintf("./output/render_%d.png", i)
// Write the output from the template to a file
ioutil.WriteFile("./render.html", outBytes, 0644)
// Use headless Chrome to open the HTML template, take a screenshot, and save to the render file
screenshot(renderFile, fname)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment