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))
def test_generate_pdf_chrome_path_success(self): from chromepdf import generate_pdf # top-level, not via chromepdf.shortcuts html = 'One Word' pdfbytes = generate_pdf(html, chrome_path=findChromePath()) self.assertIsInstance(pdfbytes, bytes) self.assertEqual(1, extractText(pdfbytes).count(html))
def test_generate_pdf_maker_with_pdf_kwargs(self): """Test outputting a PDF using a ChromePdfMaker object, and passing pdf_kwargs.""" html = 'One Word' pdfmaker = ChromePdfMaker() pdfbytes = pdfmaker.generate_pdf(html, clean_pdf_kwargs()) self.assertIsInstance(pdfbytes, bytes) self.assertEqual(1, extractText(pdfbytes).count(html))
def test_generate_pdf(self): """Test outputting a PDF using the generate_pdf() shortcut function.""" from chromepdf import generate_pdf # top-level, not via chromepdf.shortcuts html = 'One Word' pdfbytes = generate_pdf(html) self.assertIsInstance(pdfbytes, bytes) self.assertEqual(1, extractText(pdfbytes).count(html))
def test_generate_pdf_chromedriver_path_success(self): from chromepdf import generate_pdf # top-level, not via chromepdf.shortcuts chromedriver_path = download_chromedriver_version( get_chrome_version(findChromePath())) html = 'One Word' pdfbytes = generate_pdf(html, chromedriver_path=chromedriver_path) self.assertIsInstance(pdfbytes, bytes) self.assertEqual(1, extractText(pdfbytes).count(html))
def test_generate_pdf_huge_pdfs(self): """Test outputting PDFs when the HTML is 1 MB.""" from chromepdf import generate_pdf # top-level, not via chromepdf.shortcuts html = '123456789 ' * ((1000 * 1000) // 10) # 10 bytes * 1 MB = 1 MB # generate_pdf pdfbytes = generate_pdf(html) self.assertIsInstance(pdfbytes, bytes) extracted_text = extractText(pdfbytes).strip() self.assertEqual(1000 * 100, extracted_text.count('123456789'))
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)
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))