mpdf: Get the PDF file URL? - mpdf

With mpdf, the following outputs the PDF inline to the browser:
$mpdf->Output();
How can I simply get the full URL of the created file? For example:
// http://example.com/tmp/file.pdf
$url = $mpdf->getURL();

When outputting the file to the browser, the output file name is the file name of your script, no physical PDF file is being created and saved anywhere.
Therefore you could use something like
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
as answered in this QA.
Note that this cannot be used when you rely on HTTP POST method to generate the PDF.
If you need to save and provide static pdf file for later download, use F (or helper constant \Mpdf\Output\Destination::FILE in mPDF 7.x) output mode with path to desired file in the Output method:
$filename = __DIR__ . '/download.pdf';
$mpdf->Output($filename, 'F');
You then have to create the URL to the file yourself as mPDF does not know anything (or care) about your domain, document root and path to the file-to-be-downloaded.
Also see documentation on the Output method.

Related

Drupal 7, hook_file_presave display error message and stop uploading file

I am new to drupal 7, I am trying to create hook_file_presave / hook_file_validate / hook_field_validate function, That checks if pdf file that is uploaded on site is not password protected.
I can easily check using php if file is password protected. But when I display error message, it only displays error message also uploads file. I think i am not using right hook.
function simpletest_file_presave($destination){
// here is my logic
drupal_set_message(t('file is encrypted >>>>>>>> '. $filename), 'error');
return;
}
Here you can see file shouldn't be uploaded buit its there with remove button.
It's been a while since I've touched Drupal, but I first thought about hook_file_validate().
But as explained here you could implement hook_form_FORM_ID_alter() in order to add your own file upload validator and then return an array of errors to display.
Important: When you use the t() function to translate your message don't append the filename to your string because this will create multiple translated strings, one for each uploaded file so it will never be translated as it will always vary. To avoid that, use a placeholder and pass the filename as string parameter, like this:
$error_message = t(
'The file "#filename" is encrypted! Please upload a PDF without password protection.',
['#filename' => $filename_without_path]
);
See the API documentation for the t() and format_string() functions

mPDF to output a local file + file inline to the browser

I wanna know if i can output a pdf, using mPDF, to a destination as well as a preview in the browser at the same time. ie. I+F at the same time.
I: send the file inline to the browser. The plug-in is used if
available. The name given by filename is used when one selects the
"Save as" option on the link generating the PDF.
F: save to a local
file with the name given by filename
My current code is like this:
$mpdf->Output($OutputLocation,F);
$mpdf->Output();
exit;
It can export the pdf file to $OutputLocation successfully, however, in the browser, it shows:
mPDF error: Some data has already been output to browser, can't send
PDF file
In the documents of mPDF, it has only show one option for one time, any methods to do both in the same output process?
http://mpdf1.com/manual/index.php?tid=125
Please help, thanks.
It turns out that it is an issue on QNAP that their default php.ini is not allowing output buffering.
To solve this, I have edited the php.ini in the WebUI and change the output buffering to 4096. Then it works again.
output_buffering = 4096

Getting extension of the file in FileUpload Control

At the moment i get file extension of the file like :
string fileExt = System.IO.Path.GetExtension(filUpload.FileName);
But if the user change the file extension of the file ( for example user could rename "test.txt" to "test.jpg" ), I can't get the real extension . What's the solution ?
You seem to be asking if you can identify file-type from its content.
Most solutions will indeed attempt the file extension, but there are too many different possible file types to be reliably identifiable.
Most approaches use the first several bytes of the file to determine what they are.
Here is one list, here another.
If you are only worried about text vs binary, see this SO question and answers.
See this SO answer for checking if a file is a JPG - this approach can be extended to use other file headers as in the first two links in this answer.
Whatever the user renames the file extension to, that is the real file extension.
You should never depend on the file extension to tell you what's in the file, since it can be renamed.
See "how can we check file types before uploading them in asp.net?"
There's no way to get the 'real' file extension - the file extension that you get from the filename is the real one. If file content is your concern, you can retrieve the content type using the .ContentType property and verify that it is a content type that you are expecting - eg. image/jpg.

ASP.NET localized files

I've got a web page with a link, and the link is suppose to correspond to a PDF is the given user's language. I'm wondering where I should put these PDF files though. If I put them in App_LocalResources, I can't specify a link to /App_LocalResources/TOS_en-US.pdf can I?
The PDF should definitely not be in the App_LocalResources folder. That folder is only for RESX files.
The PDF files can go anywhere else in your app. For example, a great place to put them would be in a ~/PDF folder. Then your links will have to be dynamically generated (similar to what Greg has shown):
string cultureSpecificFileName = String.Format("TOS_{0}.pdf", CultureInfo.CurrentCulture.Name);
However, there are some other things to consider:
You need a way to ensure that you actually have a PDF for the given language. If someone shows up at your site and has their culture specified as Klingon, it's unlikely that you have such a PDF.
You need to decide exactly what the file format will be. In the example given, the file would have to be named TOS_en-US.pdf. It you want to use the 2-letter ISO culture names, use CurrentCulture.TwoLetterISOLanguageName and then the file name would be TOS_en.pdf.
I would store the filename somewhere with an argument in it (i.e. "TOS_{0}.pdf" ) and then just add the appropriate suffix in code:
string cultureSpecificFileName = string.Format("TOS_{0}.pdf", CultureInfo.CurrentCulture);
Does the PDF have to have the same file name for each of the different languages? If not, put them all into a directory and just store the path in your resources file.

checking for the file type(.txt,jpg,etc) and size before uploading

i am trying to uplaod the file . before that i need to check for the file type and size before saving into the specified folder.
i need allow user only upload .jpg, .bmp, .swf,.png,.tiff,
no other fiel like .txt, pdf, .doc and need to check file size is always less than 1 MB. can we do this in javascript or c# coding
2: and before saving the file i need to check if there is any file with a same name in the folder if it is there than alret the user telling a file name exits
and should rename the file
any solution on this would be great
thank you
As for checking the file size and extension prior to uploading, you'll need to use some form of client side control for such. I'd recommend something like http://swfupload.org/.
As for checking to see if the same file name exists on the server prior, you'll need to use one of the pre-upload events from such a component to make an ajax call to the server to verify such.
You can use regular expression to check file type
<asp:RegularExpressionValidator ID="rexpImageE" Display="Dynamic" runat="server"
ControlToValidate="fup1" ErrorMessage="Only .gif, .jpg, .jpeg, .png, .tiff"
ValidationExpression="(.*\.([Gg][Ii][Ff])|.*\.([Jj][Pp][Gg])|.*\.([Bb][Mm][Pp])|.*\.([pP][nN][gG])|.*\.([tT][iI][iI][fF])$)"></asp:RegularExpressionValidator>
and you can check file size on server side like
if (fup1.PostedFile.ContentLength > lengthInBytes)
{
//your message
return;
}
You can only check filename and size after the file is sent to the server in C#.
You can use the FileName property to check the name. To get the file's extension, you can write Path.GetExtension(upload.FileName). Note that the fact that the file's extension is jpg doesn't mean that it's actually a JPEG image.
To check whether the file already exists, write File.Exists(Path.Combine(#"Your folder", upload.FileName))
To get the size in bytes, check upload.PostedFile.ContentLength.
You can find the correct way to do this on MSDN.
Here's a quick snippet which checks the file type:
if (bannerImageUpload.HasFile)
{
if (bannerFileExt == ".jpg")
{
Stream bannerFileStream = bannerImageUpload.PostedFile.InputStream;
bannerFileData = new byte[bannerImageUpload.PostedFile.ContentLength];
bannerFileStream.Read(bannerFileData, 0,
bannerImageUpload.PostedFile.ContentLength);
}
}
It's probably easier to use a RegularExpressionValidator to do this on the client. Note the use of the ContentLength property. Use File.Exists to check directory folder for any existing file with same name as explained by SLaks :-)

Resources