Managing Uploaded PDFs
Once the PDFs have already been uploaded to the server, the way that you interact with these uploaded files is the same (regardless of which method they were uploaded). The main things you are able to do with the PDFs on the server are: preview, download, or clear PDFs. It is very important to note that the ONLY pdf files that you can interact with in these ways are the files uploaded to THAT SERVER. Meaning that if a file was uploaded on another server domain, it will not be able to be accessed from another. For example, apps.bennerlibrary.com has one set of PDFs in the pdf-folder
, and library.olivet.edu/info-apps has another. So it is very important that for whichever PDF you are attempting to download, that you are doing it from the actual server on which it was uploaded. The easiest way to make sure this happens is to handle the PDF within the same function as the one that creates it.
Viewing PDFs
For the first two actions you can do with the PDFs that have been uploaded, it is done the same way, using simply a different view helper. If you need a refresher, an introductory discussion of the view module is discussed in our main FW Docs, found here. The two new PDF view helpers that are available with PDF Core are: pdf-inline
and pdf-download
. Each are described below:
pdf-inline
is a view that allows the user to preview the PDF in the browser. From there the user is able to download, save or edit the PDF (to the extent that a Chrome or Firefox browser allows you to take these actions on any PDF that you view in your browser).pdf-download
is a view that treats the pdf as an attachment, and immediately downloads it (without a preview) to your computer's default download folder.
Within the apps, both of these views are used on different occasions to serve different purposes. In each case, they are rendered exactly the same, one just needs to use the following code:
$this->view->helper($viewType)
->loadPdf($pdfSavedName, $pdfNewName)
->render();
In this, each of the variables are as follows:
$viewType
is obviously eitherpdf-inline
orpdf-download
depending upon which option you desire to use.$pdfSavedName
is the name the file you are seeking to view or download in order for it to be found. This needs to include any sub-directories you have put it in after thepdfDirectory
(for example if you have a folder for each department in thepdfDirectory
, you will need to include the department as well).$pdfNewName
is the name that will be used for the file from now on, usually a more simplified version (as this does not have to be unique, since it will just be viewed and/or downloaded by the end user). This will be the name of the file that will be downloaded or title displayed in the browser when you preview it.
Here is an example of each:
// the code to preview the application that was uploaded and saved by the end user from Job App
$this->view->helper('pdf-inline')
->loadPdf("{$dept->get('dept')}/{$options->dept}-supplementary-application.pdf",
"{$deptName}_Application.pdf")
->render();
// the code to download the generated pdf of the point cards by Tally
$this->view->helper('pdf-download')
->loadPdf("{$dept}/cards-{$last}.pdf", 'cards.pdf')
->render();
Note: the pdf-inline
view cannot be returned from the same function that creates or uploads the file, whereas pdf-download
can be. It is also suggested that you use the URL and open it in a new tab. The Javascript used for previewing a file previously uploaded is as follows:
let previewUrl = this.get('config').formUrl('pdfController', 'previewPdf');
window.open(previewUrl, '_blank');
Note that it is using the config
service to generate the url (which must be properly defined in Router.php
), and then it is opening that URL in a new tab.
Clearing PDFs
The final action you are able to take upon the uploaded PDFs is to delete files that are already saved. There are two ways to do this: either to delete a single file or a batch delete of all files within a specific directory. Here are the functions to do that:
PdfUtils::clearPdf($fileName)
will delete a single PDF file, so long as$fileName
includes all sub-directories ofpdfDirectory
that it would use to find the file.PdfUtils::clearPdfsFromDir($dirName)
will batch delete all PDFs from the$dirName
specified, which should be a subdirectory ofpdfDirectory
.
For example:
//In Job App, they deleted supplementary application PDFs individually in the following way:
$name = "$dept\\$jobDeptId-supplementary-application.pdf";
PdfUtils::clearPdf($name);
//In Tally, the subdirectories within the pdfDirectory is the department short code of the department
// the pdf was saved from, so to delete all pdfs for that department, it used:
PdfUtils::clearPdfsFromDir($this->user->get('currentDept'));
Finding Saved PDFs without the Name
While it is usually preferable to have the name of the file you want to do things with, there is a recourse to see all the names of the file within a subdirectory of your set pdfDirectory
, which will return an array of all the file names, so that you might be able to find a file that you do not know the name exactly. This should be used rarely, as it is much better to save the exact file name somewhere on the database, but if it is ever needed, the function is PdfUtils::getPdfsFromDir($dirName)
, where dirName
is a subdirectory of your pdfDirectory
setup in settings. There is no way currently to return all pdfs in your pdfDirectory
irrespective of subdirectory.