@@ -5,6 +5,8 @@ package application
55
66import (
77 "context"
8+ "fmt"
9+ "os"
810 "time"
911
1012 "github.com/BuddhiLW/AutoPDF/pkg/api"
@@ -65,7 +67,11 @@ func (s *PDFGenerationApplicationService) GeneratePDF(ctx context.Context, req g
6567 for k , v := range simpleVariables {
6668 interfaceVars [k ] = v
6769 }
68- _ , err = s .templateService .Process (ctx , req .TemplatePath , interfaceVars )
70+
71+ // Debug: Log variables being processed
72+ fmt .Printf ("DEBUG: Processing template with variables: %+v\n " , interfaceVars )
73+
74+ processedContent , err := s .templateService .Process (ctx , req .TemplatePath , interfaceVars )
6975 if err != nil {
7076 return generation.PDFGenerationResult {
7177 Success : false ,
@@ -79,10 +85,52 @@ func (s *PDFGenerationApplicationService) GeneratePDF(ctx context.Context, req g
7985 }, err
8086 }
8187
82- // Step 4: Generate PDF using external service
88+ // Write processed template to a temporary file
89+ // This ensures the LaTeX engine uses the processed content with variables replaced
90+ tempFile , err := os .CreateTemp ("" , "autopdf-processed-*.tex" )
91+ if err != nil {
92+ return generation.PDFGenerationResult {
93+ Success : false ,
94+ Error : domain.TemplateProcessingError {
95+ Code : domain .ErrCodeTemplateInvalid ,
96+ Message : "Failed to create temporary file for processed template" ,
97+ Details : api .NewErrorDetails (api .ErrorCategoryTemplate , api .ErrorSeverityHigh ).
98+ WithTemplatePath (req .TemplatePath ).
99+ WithError (err ),
100+ },
101+ }, err
102+ }
103+ defer os .Remove (tempFile .Name ()) // Clean up temporary file after generation
104+
105+ // Debug: Log processed content
106+ contentLen := len (processedContent )
107+ if contentLen > 500 {
108+ contentLen = 500
109+ }
110+ fmt .Printf ("DEBUG: Processed template content (first %d chars): %s\n " , contentLen , processedContent [:contentLen ])
111+
112+ if _ , err := tempFile .WriteString (processedContent ); err != nil {
113+ tempFile .Close ()
114+ return generation.PDFGenerationResult {
115+ Success : false ,
116+ Error : domain.TemplateProcessingError {
117+ Code : domain .ErrCodeTemplateInvalid ,
118+ Message : "Failed to write processed template to temporary file" ,
119+ Details : api .NewErrorDetails (api .ErrorCategoryTemplate , api .ErrorSeverityHigh ).
120+ WithTemplatePath (req .TemplatePath ).
121+ WithError (err ),
122+ },
123+ }, err
124+ }
125+ tempFile .Close ()
126+
127+ // Debug: Log temporary file path
128+ fmt .Printf ("DEBUG: Using temporary template file: %s\n " , tempFile .Name ())
129+
130+ // Step 4: Generate PDF using external service with processed template
83131 generationReq := generation.PDFGenerationRequest {
84- TemplatePath : req . TemplatePath ,
85- Variables : req .Variables , // Use original complex variables
132+ TemplatePath : tempFile . Name (), // Use processed template file
133+ Variables : req .Variables , // Keep original variables for metadata
86134 Engine : req .Engine ,
87135 OutputPath : req .OutputPath ,
88136 Options : req .Options ,
0 commit comments