Compressed XML with PHP "gzdeflate" not working with draw.io "pako.inflateraw"

231 views
Skip to first unread message

glaucco

unread,
Jan 27, 2018, 7:03:14 AM1/27/18
to draw.io

Hi,

I'm trying to manipulate the XML of the diagram on the server side in order to add some addtional info that will be shown in the default tooltip of some objects.

So far I've managed to decompress the XML with PHP and made all the updates needed.
I then compress the XML and send it to the client side, but the draw.io viewer gives this eror message:

"Uncaught invalid distance too far back"

This is the PHP code I'm using to compress and encode the XML:

$a = urlencode(trim(explode("\n", $diagramXml->asXML(), 2)[1]));
$b = gzdeflate($a, -1, ZLIB_ENCODING_RAW);

//Unsigned integer block size
$b = unpack("I*", $b);

//Convert to string
$compressedXml = base64_encode($this->bytesToString($b));

private function bytesToString($arr)
{
$str = '';

for ($i = 1; $i <= sizeof($arr); $i++) {
$char = '\u' . $arr[$i];
$str = $str . json_decode('"'.$char.'"');
}
return $str;
}


If I don't change the compressed XML on the server side all things are working so that excludes any client-server communication related issue.

Have been scratching my head around this for the past week without success... any help would be great!

Thanks in advance!

David Benson

unread,
Jan 27, 2018, 8:04:03 AM1/27/18
to draw.io

glaucco

unread,
Jan 27, 2018, 8:41:25 AM1/27/18
to draw.io
Hi David,

Thanks for the info but I think the problem is related with the bytes to string conversion after the gzdeflate.
Already changed to rawurlencode and the error message is the same.

Thanks again!

glaucco

unread,
Jan 31, 2018, 6:29:51 AM1/31/18
to draw.io
Problem solved!

The bytesToString function was causing the error.

The correct steps are:

// 1. url encode
$a = rawurlencode(trim(explode("\n", $diagramXml->asXML(), 2)[1]));

// 2. deflate
$b = gzdeflate($a, -1, ZLIB_ENCODING_RAW);

// 3. base64 encode
$c = base64_encode($b);
Reply all
Reply to author
Forward
0 new messages