I'm trying to set up a way to index uploaded PDFs in such a way that a user can search for a term (foo) and get a list of all the nodes with fields that contain foo somewhere; and once they've clicked through to a page, they should be able to search for bar within the attached PDF and see a list of snippets along with a link to jump to that page in the PDF. In my research, I decided the best way to do this would be to write a module that splits the PDFs into individual pages with filenames that include the page number, index those new documents (along with the parent document), and build my "snippet" results from those new documents. So, for example, when I upload sample.pdf, I get the following folder structure:

 +-- sample.pdf
 +-- sample.pdf_src
 |  |  
 |  +-- sample.pdf_1.pdf
 |  +-- sample.pdf_2.pdf
 |  \-- sample.pdf_3.pdf

That works fine, but where I'm getting stuck is indexing those extra files. For background, I'm using Acquia's Solr module (apachesolr_search) with the extra attachments module (apachesolr_attachments). Is it even possible to manually index those additional files I broke out? If not, are there any other ways I can get those files into the index? Maybe attach them to the original node where the user uploaded the original PDF? I just don't want to get into a situation where I have 800 attachments to a single node because the one PDF the user uploaded contained 800 pages.

If it were possible to get page numbers out of the original upload when searching for content, that would be the most ideal situation; however, my research indicates it's not possible. I'd be happy to be proven wrong, though.


Decided just to work within the Drupal framework rather than trying to hard-code something special. After I split the PDF, for each page I used file_save_data() to register the file in the file_managed table, then added the resulting file to the node in a separate file field:

$thisSplitURI = "{$splitURI}/{$finalFilename}_{$i}.pdf";
$thisSplitPath = drupal_realpath($thisSplitURI);
$file = file_save_data($contents, $thisSplitURI); // $contents is the value where the raw PDF contents are stored
$file->display = 1;
$node->field_split_pdf_pages[LANGUAGE_NONE][] = (array) $file;

Probably not ideal, especially when there's an 800-page document in there, but it gets the job done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.