Zend Mail - Pdf Attachment - imbedded png gets scrambled - zend-framework

I've got my Zend_Mail setup and working fine, but the png images in my pdf attachment get scrambled.
This is the bit responsible for the attachment:
$filecontents = file_get_contents($attachment);
$att = $this->mail->createAttachment($filecontents);
$att->disposition = 'Zend_Mime::DISPOSITION_INLINE';
$att->encoding = 'Zend_Mime::ENCODING_BASE64';
$att->filename = 'result.pdf';
$att->type = 'application/pdf';
The Pdf is created with Zend_Pdf
The PNG is added like this:
$image = Zend_Pdf_Image::imageWithPath("path/to/png.png");
$this->page->drawImage($image, $left, $bottom, $right, $top);
Any ideas?
Thanks in advance!
Peter

Do you actually want the PDF to be inline in the email? I don't think email clients even support that. Try Zend_Mime::DISPOSITION_ATTACHMENT.
Also, the constants should not be in quotes. It's just $at->disposition = Zend_Mime::DISPOSITION_ATTACHEMENT.

Why do you set inline disposition for a PDF attachment? The mail client can't display it inline anyway (unlike the GIF in the Zend example).
The PNG format contains a \r\n in its signature, so I'd check for corruption there.. possibly triggered by the disposition:INLINE setting, but perhaps you've enabled some stream wrappers you're not telling us about?

Related

TagLib - Extract Album Art from MP3 (Powershell)

So, I'm using TagLib to make a quick batch editor for all my MP3 files to change their tags all at once. The problem I'm facing is how do I extract the original album art from the file? For now I only know I can change the album art with the following code:
$media.tag.pictures = [taglib.picture]::createfrompath("pic.jpg")
But how do I get the original one from the source file? Thanks in advance!
I have found the solution, in case anyone also wants to achieve this with Powershell! Many thanks to #DiamondDrake for sharing this link written in C# which taught me what I needed.
So it should be done likes this:
[Reflection.Assembly]::LoadFrom("taglib-sharp.dll") #Load your taglib-sharp.dll
$media = [TagLib.File]::Create("song.mp3") #Make a variable from your song file
$albumart = $media.tag.pictures[0].Data.Data #Where 0 is the first album art
$label.BackgroundImage = $albumart #I used it as a background for a label
$label.BackgroundImageLayout = 'Stretch' #And stretched it to be seen clearly

Additional PDF Attachment in E-Mail (ABAP)

I'm currently trying to send the results of a selection via E-Mail, more precisely as an attachment. My goal is to create a XML-File (which works so far) and a PDF, both fed from the internal table in which the selected data is held. The internal table is declared with a custom type. My current code for sending the E-Mail with the XML attachment looks like following:
lr_send_request = cl_bcs=>create_persistent( ).
lr_document = cl_document_bcs=>create_document( i_type = 'HTM'
i_text = lt_text
i_subject = lv_subject ).
* ----- converting data of internal table so it is suitable for XML
...
* -----
lr_document->add_attachment( i_attachment_type = 'BIN'
i_attachment_subject = 'output.xml'
i_attachment_size = xml_size
i_attachment_language = sy-langu
i_att_content_hex = xml_content ).
lr_send_request->set_document( lr_document ).
On the web I was only able to find how to convert spooljob (whatever that is :/) into PDF. With functions like that I may be able to solve my problem but then I can't attach the XML anymore.
How can I convert the data of the internal table into a PDF file to attach it to the E-Mail in the same way I do with the XML?
There are multiple way to create PDF:
Create report with Smartform and get output in PDF format. Sample code
If your system has adobe form license create with adobe form.
Use zcl_pdf class for creating native pdf file.
Using CONVERT_ABAPSPOOLJOB_2_PDF FM for getting printer spool as pdf (thanks #Sandra Rossi).
If your PDF is simple (not include complex table, vertical text, images, etc) use third option, otherwise try first or second.

Handling multiple encodings in a single file

I'm encountering some weird encoding issues. I need to parse an HTML document from the web, and I'm using the 'Content-Type' charset meta-data to determine the encoding type.
One page has been giving me trouble and is encoded by 'Shift_jis' (Japanese) - The parser result contains some garbled characters.
When I parse the same document using UTF-8 the characters that were garbled before are parsed correctly but everything else is now garbled.
I'm assuming the document contains text in two different encoding types.
I there anyway I could parse this document correctly ?
Also, I don't how, but all the browsers seem to deal well with the issue and are presenting the page nicely.
Would really appreciate any thoughts on this.
The page that I need to parse : http://ao.recruit.co.jp/form.html
First of all, what the browser sees is:
莨夂、セ讎りヲ
What is shown in rendered html is not the same because of the CSS text-indent: -9999px and the background image laid over it. But it's there. Removing them will show the text browser is seeing.
Out of the box, decoding as Shift-Jis should give you 莨夂、セ讎りヲ?, but if you want same results as in a browser, you should use a custom CharsetDecoder with IGNORE:
URL url = new URL( "http://ao.recruit.co.jp/form.html");
BufferedInputStream bis = new BufferedInputStream(url.openStream());
CharsetDecoder decoder = Charset.forName("Shift-Jis").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
Reader inputReader = new InputStreamReader(bis, decoder);
String result = IOUtils.toString(inputReader);
System.out.print(result);
This will give you same result as with browsers. Of course, it won't parse the text from the image file.

ITextSharp - Adding a Watermark; Leaving the PDF editable - FormFlattening = false

We have a large amount of PDF files that we are creating a web site to allow users to download them and when they do we want to:
Put a watermark on it with their name.
We want the form fields to be left open so they can enter their information.
We want to be able to print and save the document
When I put the Watermark on the document and then open it I get a message from Adobe:
"The document has been changed since it was created and use of extended features is no longer available. Please contact the author..."
According the book "iText-in-Action", this is a security issue (Chapter 8). There seems to be 2 ways to open them:
Remove usage rights : This breaks # 3 above.
Open it in append mode : It does not matter if modify it and save it with "FormFlattening = false" or true, if I put a water mark on the form the fields are no longer editable.
The error message from Adobe does describe the problem, I have modified the content of the document with the watermark, and the form fields become blocked because of this.
I have tried opening the document putting the watermark on it and saving it to a new file, and then closing it. Then reopening it and the trying to unblock the form fields, but it does not work.
Does anyone know if this is possible?
I have read something about templates; I don't know if this is a solution because of the work to convert the documents to templates? Does anyone know if this would help?
Below is a sample of my code for using an Image as a watermark, although I have tried adding text as well:
PdfReader reader = new PdfReader(sourceFile.FullName);
//reader.RemoveUsageRights();
var fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite);
PdfStamper pdfStamper = new PdfStamper(reader, fileStream, '\0', true);
Image image = Image.GetInstance(imagePath);
image.SetAbsolutePosition(250, 300);
for (int i = 1; i <= reader.NumberOfPages; i++) // Must start at 1 because 0 is not an actual page.
{
PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
pdfPageContents.AddImage(image);
}
pdfStamper.FormFlattening = false; // enable this if you want the PDF flattened.
//bool have = pdfStamper.PartialFormFlattening("test");
pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream.
A document that is Reader-enabled is digitally signed using a private key owned by Adobe. If Adobe Reader can validate that signature using Adobe's public key, the extra functionality (e.g. allowing you to save a form that has been filled out) is enabled.
Adding a watermark isn't part of the actions you're allowed to do with a digitally signed document. There is absolutely no way you can achieve what you want without invalidating the digital signature that triggers the reader enabling.
In short: you're trying to do something that is impossible. You can only achieve this by using Adobe software because you need Adobe's private key to 'restore' the reader enabling after breaking it.
Good advice.
Every time I reboot my computer Adobe complains about needing to be updated. Last thing I want is to be stuck with a hack, that may not work in the future.
One of my attempts was to create a watermark on a different layer of the PDF in the hopes that it would not see it as changing the text layer of the PDF, but this did not work. My boss had a thought of grabing the text from the original PDF and coping it to a new document and then putting the watermark on it. even though I created a new PDF it still sees it as modifying it, and the fields are still not editable.
Still stuck

PDF file stored as BLOB, view in a webpage perl

I have a code that handles displaying a blob from a local Oracle database. I store both JPG and PDF files as blob. I could view the JPG file, but not the PDF. I have checked these
$self->content_type('image/jpg')
to
$self->content_type('application/pdf').
And the Blob does have data. I checked the length and it has "184546".
All I get when I click the link for the pdf file is a blank page with the title GETIMAGPAGE(application/pdf).
Any help or pointers would be greatly appreciated.
Also, How can we have the content_type to enable two different mime_types? For example in my case both image as well as pdf, depending on what we get?
File::MMagic can recognize the type of data using magic numbers.
use File::MMagic;
$magic = File::MMagic->new;
$self->content($blob);
$self->content_type($magic->checktype_contents($blob));
If you don't want to require a native/plugin PDF reader, perhaps FlexPaper might fit your needs.