diff --git a/modules/forms_api/app/services/forms_api/pdf_filler.rb b/modules/forms_api/app/services/forms_api/pdf_filler.rb index a9666880aa3..840cf7a26b4 100644 --- a/modules/forms_api/app/services/forms_api/pdf_filler.rb +++ b/modules/forms_api/app/services/forms_api/pdf_filler.rb @@ -31,12 +31,22 @@ def mapped_data b = binding b.local_variable_set(:data, data) result = ERB.new(template).result(b) - JSON.parse(result) + JSON.parse(escape_json_string(result)) end def metadata klass = "FormsApi::#{form_number.titleize.gsub(' ', '')}".constantize.new(data) klass.metadata("tmp/#{form_number}-tmp.pdf") end + + def escape_json_string(str) + # remove characters that will break the json parser + # \u0000-\u001f: control characters in the ASCII table, + # characters such as null, tab, line feed, and carriage return + # \u0080-\u009f: control characters in the Latin-1 Supplement block of Unicode + # \u2000-\u201f: various punctuation and other non-printable characters in Unicode, + # including various types of spaces, dashes, and quotation marks. + str.gsub(/[\u0000-\u001f\u0080-\u009f\u2000-\u201f]/, ' ') + end end end diff --git a/modules/forms_api/spec/fixtures/form_json/vba_26_4555.json b/modules/forms_api/spec/fixtures/form_json/vba_26_4555.json index 88ab577c23b..74f8f1fb28d 100644 --- a/modules/forms_api/spec/fixtures/form_json/vba_26_4555.json +++ b/modules/forms_api/spec/fixtures/form_json/vba_26_4555.json @@ -59,5 +59,5 @@ "postal_code": "54890" } }, - "remarks": "Lengthy remarks" -} \ No newline at end of file + "remarks": "Lengthy remarks here \nabout what is needed\tand such" +}