Code Monkey home page Code Monkey logo

Comments (5)

lukelex avatar lukelex commented on July 24, 2024

@nmenag can you provide an example code that reproduces the error?

from htmltoword.

kreintjes avatar kreintjes commented on July 24, 2024

Same problem over here. I am using Rails 5.0.4, Ruby 2.4.1 and htmltoword 1.0.0 with nokogiri 1.8.3 (the exception is raised by Nokogiri).

My controller action looks like: respond_with(@report, filename: "#{Report.model_name.human.downcase}_#{@report.name.parameterize.underscore}.docx")

My Word template show.docx.slim looks like: = image_tag('logo.png')
(I'm using Slim, but don't think that causes the issue)

I also tried with an external image, i.e: img src="https://www.seconddegree.nl/wp-content/uploads/2016/01/mixed-logo-300x250.jpg"

Tried downgrading to Nokogiri 1.7.2 as well, same problem. It looks like it is impossible to include an image in the Word document at the moment. Any help would be appreciated!

from htmltoword.

kreintjes avatar kreintjes commented on July 24, 2024

Okay, found the problem. Apparently the converter (images/image_functions xslt template) requires a fixed with and height to be set, either in px or ems. So adding a style="width: 100px; height: 100px;" attribute to the img tag 'fixes' the problem.

Also the src of the image should be an absolute path pointing to the image. So if you use the asset pipeline image_tag('foobar.png') will render an image in your template, but it doesn't point to a real image on disk, so you get another error: No such file or directory @ rb_sysopen

Using an absolute URL, either /Users/.../app/assets/images/logo.png or https://www.seconddegree.nl/wp-content/uploads/2016/01/mixed-logo-300x250.jpg fixes the crash.

Now I can download an actual .docx file, but I still don't see any image inside it. It is as if the img tag is completely ignored. Am I still doing something wrong? Again, any help would be appreciated!

Ps. The Word file does become much larger when including images (369 kB instead of 20 kB), so it looks like the images are downloaded/included, but I still don't see them.

from htmltoword.

fran-worley avatar fran-worley commented on July 24, 2024

@kreintjes it was a while back when I wrote this, but I purposefully left the size and file path limitations in to keep the change as simple as possible. Resolving file paths should be doable in this lib without adding any external dependencies.

Guessing the image size when none is provided would probably require an image lib like mini/image magic and we'd need to ensure that it was an optional dependency to avoid additional overhead.

Please can you post the html snippet where you're including the image? There are a number of known issues (i.e. you can't have an image inside a link).

If you could also check the examples in this file https://github.com/karnov/htmltoword/blob/master/spec/xslt_images_spec.rb as if any of these are no longer displaying that would suggest we might need to review the code for newer versions of word might.

from htmltoword.

kreintjes avatar kreintjes commented on July 24, 2024

Hi @fran-worley, thanks for your response. I could live with setting absolute paths and/or providing the image dimensions, it just wasn't clear to me at first (isn't mentioned in the readme). However, even when I do that, the image still won't render. The template I'm trying to render (it's a slim template) is:

h1 = 'The beginning'
= image_tag('/full/path/to/image/test.png', style: 'width: 300px; height: 200px;', alt: 'Test 1'
img src="http://www.seconddegree.nl/wp-content/uploads/2016/01/mixed-logo-300x250.jpg" style="width: 300px; height: 250px;" alt="Test 2"
h2 = 'The end'

But I tried all kinds of variations, for example with setting the width and height as data attributes, setting data-external to true, providing a data-filename, etc. Note: the headers and text do show up, as does almost any other elements (bold text, lists, tables, etc.).

I also tried rendering the HTML provided in the spec in a Rails console now:

[7] pry(main)> my_html = '<!DOCTYPE html>
[7] pry(main)*   <html>
[7] pry(main)*   <head></head>
[7] pry(main)*   <body>
[7] pry(main)*     <p><img src="https://placehold.it/250x100.png" alt="Fancy image description" style="width: 250px; height: 100px"></p>
[7] pry(main)*     <p><img src="https://placehold.it/250x100.png" data-filename="image.jpg" style="height:100px; width:250px; margin-left:10px"></p>
[7] pry(main)*     <p><img src="https://placehold.it/150x100.png" data-width="150px" style="width:250px; height:100px"></p>
[7] pry(main)*     <p><img src="https://placehold.it/250x150.png" data-height="150px" style="width:250px; height:100px"></p>
[7] pry(main)*     <p><img src="https://placehold.it/150x150.png" data-width="150px" data-height="150px" style="width:250px; height:100px"></p>
[7] pry(main)*     <p><img src="https://placehold.it/150x150.png" data-width="150px" data-height="150px"></p>
[7] pry(main)*     </body>
[7] pry(main)*   </html>'
=> "<!DOCTYPE html>\n" +
"  <html>\n" +
"  <head></head>\n" +
"  <body>\n" +
"    <p><img src=\"https://placehold.it/250x100.png\" alt=\"Fancy image description\" style=\"width: 250px; height: 100px\"></p>\n" +
"    <p><img src=\"https://placehold.it/250x100.png\" data-filename=\"image.jpg\" style=\"height:100px; width:250px; margin-left:10px\"></p>\n" +
"    <p><img src=\"https://placehold.it/150x100.png\" data-width=\"150px\" style=\"width:250px; height:100px\"></p>\n" +
"    <p><img src=\"https://placehold.it/250x150.png\" data-height=\"150px\" style=\"width:250px; height:100px\"></p>\n" +
"    <p><img src=\"https://placehold.it/150x150.png\" data-width=\"150px\" data-height=\"150px\" style=\"width:250px; height:100px\"></p>\n" +
"    <p><img src=\"https://placehold.it/150x150.png\" data-width=\"150px\" data-height=\"150px\"></p>\n" +
"    </body>\n" +
"  </html>"
[8] pry(main)> file = Htmltoword::Document.create_and_save(my_html, 'images_spec.docx')
=> #<File:images_spec.docx (closed)>

In the resulting Word file, still no images appear. I have attached the resulting images_spec.docx. I have tried opening it with Pages 7.1 (macos), LibreOffice 5.4.3.2 (macos) and Microsoft Word 2010 (Windows). In the latter I get an error when trying to open the document.

from htmltoword.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.