Asserts text in a PDF file. PDF can be either a URL or a file path on the local file system. @Params pdf - The URL or file path of the PDF file. text - The expected text to verify in the PDF. page - The page number of the PDF to use (optional).
(
self,
pdf,
text,
page=None,
maxpages=None,
password=None,
codec="utf-8",
wrap=True,
nav=False,
override=False,
caching=True,
)
| 7832 | return pdf_text.strip() |
| 7833 | |
| 7834 | def assert_pdf_text( |
| 7835 | self, |
| 7836 | pdf, |
| 7837 | text, |
| 7838 | page=None, |
| 7839 | maxpages=None, |
| 7840 | password=None, |
| 7841 | codec="utf-8", |
| 7842 | wrap=True, |
| 7843 | nav=False, |
| 7844 | override=False, |
| 7845 | caching=True, |
| 7846 | ): |
| 7847 | """Asserts text in a PDF file. |
| 7848 | PDF can be either a URL or a file path on the local file system. |
| 7849 | @Params |
| 7850 | pdf - The URL or file path of the PDF file. |
| 7851 | text - The expected text to verify in the PDF. |
| 7852 | page - The page number of the PDF to use (optional). |
| 7853 | If a page number is provided, looks only at that page. |
| 7854 | (1 is the first page, 2 is the second page, etc.) |
| 7855 | If no page number is provided, looks at all the pages. |
| 7856 | maxpages - Instead of providing a page number, you can provide |
| 7857 | the number of pages to use from the beginning. |
| 7858 | password - If the PDF is password-protected, enter it here. |
| 7859 | codec - The compression format for character encoding. |
| 7860 | (The default codec used by this method is 'utf-8'.) |
| 7861 | wrap - Replaces ' \n' with ' ' so that individual sentences |
| 7862 | from a PDF don't get broken up into separate lines when |
| 7863 | getting converted into text format. |
| 7864 | nav - If PDF is a URL, navigates to the URL in the browser first. |
| 7865 | (Not needed because the PDF will be downloaded anyway.) |
| 7866 | override - If the PDF file to be downloaded already exists in the |
| 7867 | downloaded_files/ folder, that PDF will be used |
| 7868 | instead of downloading it again. |
| 7869 | caching - If resources should be cached via pdfminer.""" |
| 7870 | text = self.__fix_unicode_conversion(text) |
| 7871 | if not codec: |
| 7872 | codec = "utf-8" |
| 7873 | pdf_text = self.get_pdf_text( |
| 7874 | pdf, |
| 7875 | page=page, |
| 7876 | maxpages=maxpages, |
| 7877 | password=password, |
| 7878 | codec=codec, |
| 7879 | wrap=wrap, |
| 7880 | nav=nav, |
| 7881 | override=override, |
| 7882 | caching=caching, |
| 7883 | ) |
| 7884 | if isinstance(page, int): |
| 7885 | if text not in pdf_text: |
| 7886 | self.fail( |
| 7887 | "PDF [%s] is missing expected text [%s] on " |
| 7888 | "page [%s]!" % (pdf, text, page) |
| 7889 | ) |
| 7890 | else: |
| 7891 | if text not in pdf_text: |