title: Using PHP to generate SVG with unicode characters to be included in Inkscape
body: <p>I have to make some PDF calendars, so I decided to automate the process creating the bulk of the days in SVG with PHP, saving the result and importing it in Inkscape for further graphics.</p>
  
  <p>Generally this procedure works smoothly, and I've used it many times for graphs, diagrams, etc., but now I've an issue with utf-8 characters, which I never had to use before.</p>
  
  <p>This is the basic PHP code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"&gt;
  &lt;svg width="1200" height="3600" xmlns="http://www.w3.org/2000/svg" encoding="UTF-8"&gt;
  
  &lt;?php
  $FontSize = 16;
  $LineHeight = $FontSize * 1.5;
  $DefaultColor = '000000';
  $Y = $LineHeight;
  
  date_default_timezone_set('Europe/Rome');
  setlocale(LC_TIME, "it_IT.utf8");
  
  for ($mese=1; $mese&lt;=12; $mese++) {
      $M = strftime('%B', strtotime("$mese/1/2016"));
      echo "&lt;g id=\"$M\"&gt;\n";
      echo "&lt;text x=\"0\" y=\"$Y\" style=\"font-size:{$FontSize}px;fill:#000000;fill-opacity:1;stroke-width:0\"&gt;$M&lt;/text&gt;\n";
      $Y = $Y + $LineHeight;
      for ($giorno=1; $giorno&lt;=cal_days_in_month(CAL_GREGORIAN, $mese, 2016); $giorno++) {
          $G =utf8_decode(strftime('%A', strtotime("$mese/$giorno/2016")));
          $D = strftime('%e', strtotime("$mese/$giorno/2016"));
          if (strftime('%u', strtotime("$mese/$giorno/2016"))==7) {
              $colore = 'ff0000';
          } else {
              $colore = $DefaultColor;
          }
          echo "&lt;text x=\"10\" y=\"$Y\" style=\"font-size:{$FontSize}px;fill:#{$colore};fill-opacity:1;stroke-width:0\"&gt;$D&lt;/text&gt;\n";
          echo "&lt;text x=\"40\" y=\"$Y\" style=\"font-size:{$FontSize}px;fill:#{$colore};fill-opacity:1;stroke-width:0\"&gt;$G&lt;/text&gt;\n";
          $Y = $Y + $LineHeight;
      }
      $Y = $Y + $LineHeight;
      echo "&lt;/g&gt;\n";
  }
  ?&gt;
  &lt;/svg&gt;
  </code></pre>
  
  <p>The SVG output is (snippet):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"&gt;
  
  &lt;svg width="1200" height="3600" xmlns="http://www.w3.org/2000/svg" encoding="UTF-8"&gt;
  
  &lt;g id="gennaio"&gt;
  &lt;text x="0" y="24" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt;gennaio&lt;/text&gt;
  &lt;text x="10" y="48" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt; 1&lt;/text&gt;
  &lt;text x="40" y="48" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt;venerdì&lt;/text&gt;
  &lt;text x="10" y="72" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt; 2&lt;/text&gt;
  &lt;text x="40" y="72" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt;sabato&lt;/text&gt;
  &lt;text x="10" y="96" style="font-size:16px;fill:#ff0000;fill-opacity:1;stroke-width:0"&gt; 3&lt;/text&gt;
  &lt;text x="40" y="96" style="font-size:16px;fill:#ff0000;fill-opacity:1;stroke-width:0"&gt;domenica&lt;/text&gt;
  &lt;text x="10" y="120" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt; 4&lt;/text&gt;
  &lt;text x="40" y="120" style="font-size:16px;fill:#000000;fill-opacity:1;stroke-width:0"&gt;lunedì&lt;/text&gt;
  </code></pre>
  
  <p>On the screen it works smoothly.</p>
  
  <p>However, when with Firefox I save the generated page, the utf-8 encoding is lost, and Inkscape can't deal with this. Every final "ì" of the Italian day names is replaced with a long line of undefined characters.</p>
  
  <p>When I check the "page informations" from Firefox I see the encoding is "Windows-1252", and I don't know where it comes from, while if I save the resulting page and open it with the text editor Kate I see the encoding is ISO-8859-15, and the "ì" are correctly shown in the file.</p>
  
  <p>If I check the saved file from console (<code>less file.svg</code>, with <code>XTERM_LOCALE=it_IT.utf8</code>) I see the "ì" are replaced with inverted <code>&lt;EC&gt;</code> hexcodes. U+00EC is "LATIN SMALL LETTER I WITH GRAVE", so at least it is correct.</p>
  
  <p>If I re-save the page from Kate, forcing the encoding to UTF-8, Inkscape gladly accepts it.</p>
  
  <p>If I try to replace the "ì" with the html entity <code>&amp;igrave;</code> Inkscape doesn't show the glyph.</p>
  
  <p>The question is: <strong>how can I save the page seen on Firefox as UTF-8?</strong> It seems Firefox doesn't allow to change the encoding of the files, as Kate does.</p>