Ejemplo n.º 1
0
    def test_generate_pdf_and_url_special_chars(self):
        """
        Test outputting a PDF with special characters.

        One unicode character is tested.
        Also, the Javascript escape character for multi-line strings (due to us using it in document.write(`{}`))
        """

        from chromepdf import generate_pdf, generate_pdf_url  # top-level, not via chromepdf.shortcuts

        html = 'Unicode Char: \u0394 Javascript escape character: ` Some quotes: "\''

        # generate_pdf
        pdfbytes = generate_pdf(html)
        self.assertIsInstance(pdfbytes, bytes)
        self.assertEqual(1, extractText(pdfbytes).count(html))

        # generate_pdf_url
        extracted_text = ''
        try:
            tempfile = createTempFile(html)
            tempfile_uri = pathlib.Path(tempfile.name).as_uri()
            pdfbytes = generate_pdf_url(tempfile_uri)
            extracted_text = extractText(pdfbytes).strip()
        finally:
            os.remove(tempfile.name)
        self.assertEqual(1, extracted_text.count(html))
Ejemplo n.º 2
0
    def test_generate_pdf_url_bad_file_uri(self):
        """Test outputting a PDF using the generate_pdf_url() shortcut function, with a bad file URI."""

        from chromepdf import generate_pdf_url  # top-level, not via chromepdf.shortcuts

        with self.assertRaises(ValueError):
            _pdfbytes = generate_pdf_url(
                '/bad/absolute/path/not/a/scheme.html')
Ejemplo n.º 3
0
    def test_generate_pdf_url_without_django(self):
        """Test outputting a PDF using the generate_pdf_url() shortcut function."""

        from chromepdf import generate_pdf_url  # top-level, not via chromepdf.shortcuts

        html = "This is a test"
        try:
            tempfile = createTempFile(html)
            tempfile_uri = pathlib.Path(tempfile.name).as_uri()
            pdfbytes = generate_pdf_url(tempfile_uri)
            self.assertIsInstance(pdfbytes, bytes)
        finally:
            os.remove(tempfile.name)
Ejemplo n.º 4
0
    def test_generate_pdf_url_huge_pdfs(self):

        from chromepdf import generate_pdf_url  # top-level, not via chromepdf.shortcuts

        html = '123456789 ' * ((1000 * 1000) // 10)  # 10 bytes * 1 MB = 1 MB

        # generate_pdf_url
        try:
            with tempfile.NamedTemporaryFile(delete=False) as temp:
                temp.write(html.encode('utf8'))  # 10* 100*1000 = 1 MB

                tempfile_uri = pathlib.Path(temp.name).as_uri()
                pdfbytes = generate_pdf_url(tempfile_uri)
                extracted_text = extractText(pdfbytes).strip()
                self.assertEqual(1000 * 100, extracted_text.count('123456789'))
        finally:
            os.remove(temp.name)
Ejemplo n.º 5
0
    def test_generate_pdf_url(self):
        """Test outputting a PDF using the generate_pdf_url() shortcut function."""

        from chromepdf import generate_pdf_url  # top-level, not via chromepdf.shortcuts

        html = "This is a test"
        extracted_text = ''
        try:
            tempfile = createTempFile(html)
            tempfile_uri = pathlib.Path(tempfile.name).as_uri()
            pdfbytes = generate_pdf_url(tempfile_uri)
            self.assertIsInstance(pdfbytes, bytes)
            extracted_text = extractText(pdfbytes)
        finally:
            os.remove(tempfile.name)

        self.assertEqual(1, extracted_text.count(html))