-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<img width="200" height="200" alt="" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdwb3J0PSIwIDAgMjAwIDIwMCIgd2lkdGg9IjIwMCIgaGVpZ2h0PSIyMDAiPjx0ZXh0IHg9IjAiIHk9IjIwIj5TVkcgZGF0YS11cmk8L3RleHQ+PC9zdmc+" /> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
if [ "$PDF_SERVICE_URL" = "" ]; then | ||
echo "\$PDF_SERVICE_URL has to be set." | ||
fi | ||
|
||
cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )" | ||
|
||
curl \ | ||
--fail \ | ||
--silent \ | ||
-H "Content-Type: text/html" \ | ||
--data "@index.html" \ | ||
"$PDF_SERVICE_URL/generate" \ | ||
> generated.pdf | ||
|
||
../../scripts/create_reference_or_diff.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import re | ||
|
||
from base64 import urlsafe_b64decode | ||
from urllib.parse import unquote | ||
from .errors import InvalidDataURI | ||
|
||
# Adapted from https://github.com/fcurella/python-datauri | ||
|
||
MIMETYPE_REGEX = r"[\w]+\/[\w\-\+\.]+" | ||
_MIMETYPE_RE = re.compile("^{}$".format(MIMETYPE_REGEX)) | ||
|
||
CHARSET_REGEX = r"[\w\-\+\.]+" | ||
_CHARSET_RE = re.compile("^{}$".format(CHARSET_REGEX)) | ||
|
||
DATA_URI_REGEX = ( | ||
r"data:" | ||
+ r"(?P<mimetype>{})?".format(MIMETYPE_REGEX) | ||
+ r"(?:\;name\=(?P<name>[\w\.\-%!*'~\(\)]+))?" | ||
+ r"(?:\;charset\=(?P<charset>{}))?".format(CHARSET_REGEX) | ||
+ r"(?P<base64>\;base64)?" | ||
+ r",(?P<data>.*)" | ||
) | ||
_DATA_URI_RE = re.compile(r"^{}$".format(DATA_URI_REGEX), re.DOTALL) | ||
|
||
|
||
def parse(self): | ||
match = _DATA_URI_RE.match(self) | ||
if not match: | ||
raise InvalidDataURI("Not a valid data URI: %r" % self) | ||
mimetype = match.group("mimetype") or None | ||
name = match.group("name") or None | ||
charset = match.group("charset") or None | ||
|
||
if match.group("base64"): | ||
_charset = charset or "utf-8" | ||
_data = bytes(match.group("data"), _charset) | ||
data = urlsafe_b64decode(_data) | ||
else: | ||
data = unquote(match.group("data")) | ||
|
||
return mimetype, name, charset, bool(match.group("base64")), data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
class URLFetcherCalledAfterExitException(Exception): | ||
def __init__(self): | ||
self.message = "Called URLFetchCather after it was closed." | ||
|
||
|
||
class InvalidDataURI(ValueError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pytest | ||
|
||
from pdfminer import high_level | ||
from pdf_service import pdf_service | ||
from io import BytesIO | ||
from pdf_service.data_uri import parse | ||
from pdf_service.errors import InvalidDataURI | ||
|
||
|
||
# Tests the data URI support | ||
|
||
@pytest.fixture | ||
def client(): | ||
with pdf_service.test_client() as client: | ||
yield client | ||
|
||
|
||
def test_contains_text(client): | ||
rv = client.post('/generate', | ||
data="<img width=\"200\" height=\"200\" alt=\"\" " | ||
"src=\"data:image/svg+xml;base64," | ||
"PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdwb3J0PSIwIDAg" | ||
"MjAwIDIwMCIgd2lkdGg9IjIwMCIgaGVpZ2h0PSIyMDAiPjx0ZXh0IHg9IjAiIHk9IjIwIj5T" | ||
"VkcgZGF0YS11cmk8L3RleHQ+PC9zdmc+\" />", | ||
content_type="text/html") | ||
|
||
assert 200 == rv.status_code | ||
assert 'application/pdf' == rv.content_type | ||
|
||
file = BytesIO(rv.data) | ||
text = high_level.extract_text(file) | ||
|
||
assert 'SVG data-uri' in text | ||
|
||
|
||
def test_invalid_data_uri(client): | ||
t = "data:*garbled*;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6" \ | ||
"eSBkb2cu" | ||
with pytest.raises(InvalidDataURI): | ||
parse(t) | ||
|
||
|
||
def test_non_base64_data_uri(client): | ||
t = "data:text/plain;charset=utf-8,sample" | ||
(_, _, _, _, text) = parse(t) | ||
|
||
assert text == "sample" | ||
|