Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed pdf to Word Document #1141

Open
vasundhara0494 opened this issue Sep 18, 2017 · 8 comments
Open

Embed pdf to Word Document #1141

vasundhara0494 opened this issue Sep 18, 2017 · 8 comments

Comments

@vasundhara0494
Copy link

vasundhara0494 commented Sep 18, 2017

I am not able to embed pdf files to the word document as oleObject. Neither am I able to embed text files. Excel/doc/ppt/docx/pptx files are easily embedded though. Is there any compatibility issue with embedding Non-ms Office files?


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@FBnil
Copy link

FBnil commented Oct 8, 2017

@vasundhara0494 can you give me some example code for the embedding? I suspect a mimetype problem.

@FBnil
Copy link

FBnil commented Oct 8, 2017

@vasundhara0494 Can you write a small example how you load and embed the files?

Meanwhile, can you edit ./src/PhpWord/Element/Object.php around line 73:

    public function __construct($source, $style = null)
    {
        $supportedTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx', 'pdf');
        $pathInfo = pathinfo($source);

        if (file_exists($source) && in_array($pathInfo['extension'], $supportedTypes)) {
            $ext = $pathInfo['extension'];
            if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
                $ext = substr($ext, 0, -1);
            }

            $this->source = $source;
            $this->style = $this->setNewStyle(new ImageStyle(), $style, true);
            $this->icon = realpath(__DIR__ . "/../resources/{$ext}.png");

            return $this;
        } else {
            throw new InvalidObjectException();
        }
    }

Add 'pdf' to the array of supported Ole's...
You will also need to create ./src/PhpWord/resources/pdf.png (copy it from doc.png for now)

And then try again...

@vasundhara0494
Copy link
Author

@FBnil I have made the following changes:

  • Created ./src/PhpWord/resources/pdf.png

  • Added 'pdf' in .\src\PhpWord\Element\Object.php at line 73:

public function __construct($source, $style = null)
    {
        $supportedTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx', 'pdf');
        /** rest of the function **/
    }
  • In .\src\PhpWord\Media.php inside function public static function addElement($container, $mediaType, $source, Image $image = null) around line 92 (case 'object'):
case 'object':
				if(false !== strpos($source, ".pdf"))
				{
					$target =  $source;
				}
				else{
                    $target = "{$container}_oleObject{$mediaTypeCount}.bin";
				}
                    break;

Instead of creating binary I have tried to put the pdf file as it is.

  • In .\src\PhpWord\Writer\Word2007.php around line 299, added content type of pdf:
private function registerContentTypes($media)
    {
        foreach ($media as $medium) {
            $mediumType = $medium['type'];
            if ($mediumType == 'image') {
                $extension = $medium['imageExtension'];
                if (!isset($this->contentTypes['default'][$extension])) {
                    $this->contentTypes['default'][$extension] = $medium['imageType'];
                }
            }elseif ($mediumType == 'object') {
				if (!isset($this->contentTypes['default']['pdf'])) {
                    $this->contentTypes['default']['pdf'] = 'application/pdf';
                }
				
	
				if (!isset($this->contentTypes['default']['bin'])) {
                    $this->contentTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject';
                }

            }
        }
    }
  • In .\src\PhpWord\Writer\Word2007\Part\Rels.php around line118
private function writeRel(XMLWriter $xmlWriter, $relId, $type, $target, $targetMode = '')
    {
        if ($type != '' && $target != '') {
            if (strpos($relId, 'rId') === false) {
                $relId = 'rId' . $relId;
            }
			
			if( strpos( $target, ".pdf" ) !== false ) //added by vasu
			{
				$type = 'officeDocument/2006/relationships/package';
			}
/*** rest of the function ****/

I have tried making the same changes for .docx and .pptx and was able to embed those (which was not happening previously) but it is still not happening for .pdf and .txt.

@FBnil
Copy link

FBnil commented Oct 9, 2017

@vasundhara0494 Wow, thanks for so much information. I embedded a pdf the normal way in LibreOffice, and found that the mime is as follows:

instead of application/pdf, try
application/vnd.openxmlformats-officedocument.oleObject

@vasundhara0494
Copy link
Author

@FBnil Tried that too. Not working!

@tglines
Copy link

tglines commented Nov 27, 2017

@vasundhara0494 @FBnil @RomanSyroeshko is anyone able to use PHPWord for pdf embeddings inside of Word successfully? We'd like to use this library instead of others out there but I'd like to know that it works.

@FBnil
Copy link

FBnil commented Nov 29, 2017

@tglines
I gave up last month after trying for a day. Code has to be added to phpword for this to work. It seemed straight forward, it but wasn't.

http:https://phpword.readthedocs.io/en/latest/elements.html#objects

@troosan
Copy link
Contributor

troosan commented Dec 6, 2017

this is going to be difficult to investigate for me as I'm having the following issue:
https://word.uservoice.com/forums/304942-word-for-mac/suggestions/13608015-when-importing-pdf-into-word-2016-for-mac-allow-c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants