@@ -23,23 +23,39 @@ func resourcePDF() *schema.Resource {
23
23
DeleteContext : resourcePDFDelete ,
24
24
25
25
Schema : map [string ]* schema.Schema {
26
+ "filename" : {
27
+ Description : "The path to the PDF file that will be created" ,
28
+ Type : schema .TypeString ,
29
+ Required : true ,
30
+ ForceNew : true ,
31
+ },
26
32
"header" : {
27
33
Description : "Header/title of PDF" ,
28
34
Type : schema .TypeString ,
29
- Required : true ,
35
+ Optional : true ,
30
36
ForceNew : true ,
37
+ ConflictsWith : []string {
38
+ "image_filename" ,
39
+ },
31
40
},
32
41
"content" : {
33
42
Description : "Content of PDF" ,
34
43
Type : schema .TypeString ,
35
- Required : true ,
44
+ Optional : true ,
36
45
ForceNew : true ,
46
+ ConflictsWith : []string {
47
+ "image_filename" ,
48
+ },
37
49
},
38
- "filename " : {
39
- Description : "The path to the PDF file that will be created " ,
50
+ "image_filename " : {
51
+ Description : "The image file to be converted to a PDF. Typically used for postcards " ,
40
52
Type : schema .TypeString ,
41
- Required : true ,
53
+ Optional : true ,
42
54
ForceNew : true ,
55
+ ConflictsWith : []string {
56
+ "header" ,
57
+ "content" ,
58
+ },
43
59
},
44
60
},
45
61
}
@@ -48,30 +64,25 @@ func resourcePDF() *schema.Resource {
48
64
func resourcePDFCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
49
65
var diags diag.Diagnostics
50
66
51
- pdf := gofpdf .New (gofpdf .OrientationPortrait , "mm" , gofpdf .PageSizeLetter , "" )
52
-
53
- header := d .Get ("header" ).(string )
54
- content := d .Get ("content" ).(string )
67
+ // Used for generating pdfs and also converting pdf's to images
55
68
filename := d .Get ("filename" ).(string )
56
69
57
- pdf .AddPage ()
58
- pdf .SetTitle (d .Get ("header" ).(string ), false )
59
- pdf .SetFont ("Arial" , "B" , 16 )
60
- // Calculate width of title and position
61
- wd := pdf .GetStringWidth (header ) + 6
62
- pdf .SetX ((210 - wd ) / 2 )
63
- // Title
64
- pdf .CellFormat (wd , 9 , header , "" , 1 , "C" , false , 0 , "" )
65
- // Line break
66
- pdf .Ln (10 )
67
- pdf .SetFont ("Arial" , "" , 11 )
68
- pdf .SetAutoPageBreak (true , 2.00 )
69
- // Write ze content
70
- pdf .Write (8 , content )
71
-
72
- err := pdf .OutputFileAndClose (filename )
73
- if err != nil {
74
- return diag .FromErr (err )
70
+ // If an image, convert to pdf
71
+ if imageFilename , ok := d .GetOk ("image_filename" ); ok {
72
+ err := convertImage (imageFilename .(string ), filename )
73
+ if err != nil {
74
+ defer resourcePDFDelete (ctx , d , filename )
75
+ return diag .FromErr (err )
76
+ }
77
+ } else {
78
+ // Generate content if not image
79
+ header := d .Get ("header" ).(string )
80
+ content := d .Get ("content" ).(string )
81
+
82
+ err := renderPDF (header , content , filename )
83
+ if err != nil {
84
+ return diag .FromErr (err )
85
+ }
75
86
}
76
87
77
88
outputContent , err := ioutil .ReadFile (filename )
@@ -116,3 +127,38 @@ func resourcePDFDelete(ctx context.Context, d *schema.ResourceData, meta any) di
116
127
os .Remove (d .Get ("filename" ).(string ))
117
128
return nil
118
129
}
130
+
131
+ // renderPDF converts header + content to a pdf and writes to an output file
132
+ func renderPDF (header string , content string , outputFilePath string ) error {
133
+ pdf := gofpdf .New (gofpdf .OrientationPortrait , "mm" , gofpdf .PageSizeLetter , "" )
134
+ pdf .AddPage ()
135
+ pdf .SetTitle (header , false )
136
+ pdf .SetFont ("Arial" , "B" , 16 )
137
+ // Calculate width of title and position
138
+ wd := pdf .GetStringWidth (header ) + 6
139
+ pdf .SetX ((210 - wd ) / 2 )
140
+ // Title
141
+ pdf .CellFormat (wd , 9 , header , "" , 1 , "C" , false , 0 , "" )
142
+ // Line break
143
+ pdf .Ln (10 )
144
+ pdf .SetFont ("Arial" , "" , 11 )
145
+ pdf .SetAutoPageBreak (true , 2.00 )
146
+ // Write ze content
147
+ pdf .Write (8 , content )
148
+
149
+ return pdf .OutputFileAndClose (outputFilePath )
150
+ }
151
+
152
+ // convertImage converts input image path to pdf file
153
+ func convertImage (inputFilePath , outputFilePath string ) error {
154
+ pdf := gofpdf .New (gofpdf .OrientationPortrait , "mm" , gofpdf .PageSizeLetter , "" )
155
+ pdf .AddPage ()
156
+ pdf .Image (inputFilePath , 0 , 0 , 240 , 480 , false , "" , 0 , "" )
157
+
158
+ err := pdf .OutputFileAndClose (outputFilePath )
159
+ if err != nil {
160
+ return err
161
+ }
162
+
163
+ return nil
164
+ }
0 commit comments