Skip to content

Commit

Permalink
T1795 - Special characters are wrongly encoded (#1967)
Browse files Browse the repository at this point in the history
* fix: save file as utf8

* fix: fix warning

* chore: format

* fix: solve solarcloud warning

---------

Co-authored-by: Clément <clement.charmillot@gmail.com>
  • Loading branch information
clementcharmillot and Clément committed Sep 12, 2024
1 parent 2ed1661 commit af83243
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 1 addition & 2 deletions sbc_compassion/FPDF/src/pdfcreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class PDFCreator
// through the available boxes.
protected $overflowPage; // Will be set when overflow occurs. Holds the overflown texts from the current page. => array[Text]


/**
*
*/
Expand Down Expand Up @@ -205,7 +204,7 @@ private function PreventInfiniteLoop($pdf, $emptyPage, $pagesWithoutText)
{
$pagesWithoutText++;
if($pagesWithoutText >= 4) {
if ($this->overflowTemplate != false) {
if ($this->utils->overflowTemplate) {
// Try to push remaining text on overflow page
$this->InsertOverflowPages($pdf);
} else {
Expand Down
2 changes: 1 addition & 1 deletion sbc_compassion/FPDF/src/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Text

function __construct($filename, $type)
{
$this->text = utf8_decode(file_get_contents($filename));
$this->text = file_get_contents($filename);
$this->type = $type;
}

Expand Down
22 changes: 16 additions & 6 deletions sbc_compassion/models/correspondence_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ def generate_pdf(self, pdf_name, header, text, image_data, background_list=None)
text_list = []
for t_type, t_boxes in list(text.items()):
for txt in t_boxes:
temp_img.append(
tempfile.NamedTemporaryFile("w", prefix=t_type + "_", suffix=".txt")
txt_file = tempfile.NamedTemporaryFile(
"w", prefix=t_type + "_", suffix=".txt", encoding="utf-8"
)
temp_img[-1].write(txt)
temp_img[-1].flush()
text_list.append([temp_img[-1].name, t_type])
txt_file.write(txt)
txt_file.flush()
temp_img.append(txt_file)
text_list.append([txt_file.name, t_type])

for image in image_data:
ifile = tempfile.NamedTemporaryFile(prefix="img_", suffix=".jpg")
Expand All @@ -206,7 +207,8 @@ def generate_pdf(self, pdf_name, header, text, image_data, background_list=None)

json_val = json.dumps(generated_json).replace(" ", "")

std_err_file = open(self.path_to("stderr.txt"), "w")
std_err_file_path = self.path_to("stderr.txt")
std_err_file = open(std_err_file_path, "w", encoding="utf-8")

php_command_args = ["php", self.path_to("pdf.php"), pdf_name, json_val]
if config.get("php_debug"):
Expand All @@ -223,6 +225,14 @@ def generate_pdf(self, pdf_name, header, text, image_data, background_list=None)
proc = subprocess.Popen(php_command_args, stderr=std_err_file)
proc.communicate()

if proc.returncode != 0:
with open(std_err_file_path, "r", encoding="utf-8") as stderr:
_logger.error(
"FPDF returned nonzero exit code %d. stderr:\n%s",
proc.returncode,
stderr.read(),
)

# Clean temp files
for img in temp_img:
img.close()
Expand Down

0 comments on commit af83243

Please sign in to comment.