Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Hello! I’m Fernando Agüero.

Slide 3

Slide 3 text

I work at

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Madrid Devs I help these communities ValenciaJS

Slide 7

Slide 7 text

Building Emails & PDFs with React

Slide 8

Slide 8 text

bit.ly/react-emails-pdf

Slide 9

Slide 9 text

Building Emails with React

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Emails are hard

Slide 12

Slide 12 text

Image from the Campaign Monitor Engineering Blog

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

You can always use Multi-part MIME FYI More accessible and less spammy!

Slide 16

Slide 16 text

Multi-part_MIME Multi-part mime refers to sending both an HTML and TEXT part of an email message in a single email

Slide 17

Slide 17 text

Multi-part_MIME Plain text HTML

Slide 18

Slide 18 text

September_2016 Google adds support for: • media queries • embedded stylesheets • class and ID support

Slide 19

Slide 19 text

Is not that bad now

Slide 20

Slide 20 text

Example of an email before and after responsive design

Slide 21

Slide 21 text

but…

Slide 22

Slide 22 text

HTML emails are here to stay, and you will need to build them.

Slide 23

Slide 23 text

Case study: Séntisis Emails

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

What is behind:

Slide 27

Slide 27 text

A typical email with nested tables madness

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Tables

Slide 30

Slide 30 text

Modularization Logo from the react-icons package and React

Slide 31

Slide 31 text

Components We created a component to encapsulate the complexities of the tables.

Slide 32

Slide 32 text

becomes

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Actual output

Slide 35

Slide 35 text

Components The Grid component handles the row and the cells to always generate a valid HTML table.

Slide 36

Slide 36 text

Components Nested tables get complex and can lead to non-valid HTML, causing email parsers to break the layout.

Slide 37

Slide 37 text

Empty element WTF?

Slide 38

Slide 38 text

Components The Grid component can be used as a wrapper or by using its Cell and Row components.

Slide 39

Slide 39 text

Grid.Cell and Grid.Row components

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Grid component

Slide 43

Slide 43 text

Components Once we are able to stop thinking about tables, we can move forward and continue creating our emails as a typical React application.

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Build a weather report demo email How_to

Slide 46

Slide 46 text

github.com/lang-ai/react-emails Demo_Repository Bootstrapped with create-react-app

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Usage 1. npm install 2. npm run build 3. node example/weather.js 4. Enjoy your generated email

Slide 49

Slide 49 text

npm run build Transpile our React code with Babel to get rid of the JSX and the ES6 imports

Slide 50

Slide 50 text

package.json

Slide 51

Slide 51 text

node example/weather.js Generate a forecast email

Slide 52

Slide 52 text

Behind_the_scenes Connecting the dots

Slide 53

Slide 53 text

Behind_the_scenes 1. Fetch weather API for the example 2. Create a React element with the API data by rendering it on the server

Slide 54

Slide 54 text

Behind_the_scenes 3. Add styles and the React element in a placeholder 4. Return the generated HTML 5. Save or send the email

Slide 55

Slide 55 text

example/weather.js

Slide 56

Slide 56 text

example/weather.js 1. Fetch API data

Slide 57

Slide 57 text

example/weather.js 2-4. Generate HTML

Slide 58

Slide 58 text

example/weather.js 5. Send or save the email

Slide 59

Slide 59 text

How it works

Slide 60

Slide 60 text

server/createEmail.js Optional CSS to be inlined Base HTML to add our styles and content

Slide 61

Slide 61 text

server/createEmail.js Generate our email HTML

Slide 62

Slide 62 text

server/createEmail.js Add our HTML and styles to the placeholder

Slide 63

Slide 63 text

server/email-placeholder.html This gets replaced to have our final HTML

Slide 64

Slide 64 text

Using it for real Production

Slide 65

Slide 65 text

Production • Used as an npm package • Transpiles on the npm install hook

Slide 66

Slide 66 text

Production Steps: • Create a new layout React component • Pass props to that layout • Send email

Slide 67

Slide 67 text

Usage example

Slide 68

Slide 68 text

Layouts Each email layout is a React component

Slide 69

Slide 69 text

Benefits 1. It’s a React app: same tooling, live reload 2. Remove complexity with wrappers: 3. Ability write logic in the templates

Slide 70

Slide 70 text

Building PDFs with React (kind of)

Slide 71

Slide 71 text

Case study: Séntisis Reports

Slide 72

Slide 72 text

PDFs are hard

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

PDFs are complex

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Avoid complexity

Slide 79

Slide 79 text

complexity uncertainty scope changes

Slide 80

Slide 80 text

Idea • Node • React + SSR • HTML to PDF?

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

• Open Source command line tool • Renders HTML into PDF • Uses the Qt Webkit rendering engine • Headless (does not require a display service)

Slide 84

Slide 84 text

CSS support?

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

It’s not perfect

Slide 87

Slide 87 text

Don’t expect top-notch CSS support

Slide 88

Slide 88 text

It’s not perfect but it get’s the job done

Slide 89

Slide 89 text

Evolution chart rendered in the PDF

Slide 90

Slide 90 text

Build an invoice demo PDF How_to

Slide 91

Slide 91 text

github.com/lang-ai/react-pdfs Demo_Repository Bootstrapped with create-react-app

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Usage 1. npm install 2. npm run build 3. node example/invoice.js 4. Enjoy your generated PDF

Slide 94

Slide 94 text

Behind_the_scenes Connecting the dots

Slide 95

Slide 95 text

Behind_the_scenes 1. Build your components 2. Render the HTML on the server 3. Pass the HTML to wkhtmltopdf Similar to react-emails

Slide 96

Slide 96 text

Same previous process as with react-emails

Slide 97

Slide 97 text

How it works

Slide 98

Slide 98 text

server/createPDF.js

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

Is there a better way?

Slide 101

Slide 101 text

Headless browsers

Slide 102

Slide 102 text

Headless browsers

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Q: What is the difference between Puppeteer, Selenium / WebDriver, and PhantomJS?

Slide 106

Slide 106 text

“Puppeteer only works with Chrome and it uses the latest version of Chromium. In non-testing use cases, Puppeteer provides a powerful but simple API because it’s only targeting one browser”

Slide 107

Slide 107 text

“Puppeteer bundles Chromium to ensure that the latest features it uses are guaranteed to be available.” package.json

Slide 108

Slide 108 text

Q: Ok, but why Puppeteer?

Slide 109

Slide 109 text

• CSS support • Export to PDF directly Benefits

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

Consider navigation to be finished when there are no network connections for at least 500 ms

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Auth & Cookies

Slide 114

Slide 114 text

Cookies We needed permission to get that user report data

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

We can now fetch the data and render the report for that user

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

Takeaways

Slide 119

Slide 119 text

Takeaways • React components benefits can be used outside a typical web application • “Good enough” is fine • Ship it

Slide 120

Slide 120 text

Takeaways • React components benefits can be used outside a typical web application • “Good enough” is fine • Ship it

Slide 121

Slide 121 text

fjaguero fjaguero Thanks! fjaguero.com bit.ly/react-emails-pdf