git.iko.soy/typeable/pandoc
on master help sign in
Scale images to fit the page for DOCX

Images that are bigger than the page were truncated. This will now scale them if they are larger than the page. The scale is currently hardcoded with Word "letter" defaults (page size and margins)

commitd4d7a14dddf860521473167d1dcb53d5cfab2b7b authorGrégory Bataille <gbataille.dev@gmail.com> dateMon 23 Jun 2014, 10:38 (12 years ago) parentcab1602
1 file changed+9−1
src/Text/Pandoc/Writers/Docx.hs+9−1
src/Text/Pandoc/Writers/Docx.hs+9−1
@@ -825,7 +825,7 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
825825 let size = imageSize img
826826 let (xpt,ypt) = maybe (120,120) sizeInPoints size
827827 -- 12700 emu = 1 pt
828− let (xemu,yemu) = (xpt * 12700, ypt * 12700)
828+ let (xemu,yemu) = fitToPage (xpt * 12700, ypt * 12700)
829829 let cNvPicPr = mknode "pic:cNvPicPr" [] $
830830 mknode "a:picLocks" [("noChangeArrowheads","1"),("noChangeAspect","1")] ()
831831 let nvPicPr = mknode "pic:nvPicPr" []
@@ -890,3 +890,11 @@ parseXml refArchive distArchive relpath =
890890 >>= parseXMLDoc . UTF8.toStringLazy . fromEntry) of
891891 Just d -> return d
892892 Nothing -> fail $ relpath ++ " corrupt or missing in reference docx"
893+
894+-- | Scales the image to fit the page
895+fitToPage :: (Integer, Integer) -> (Integer, Integer)
896+fitToPage (x, y)
897+ --5440680 is the emu width size of a letter page in portrait, minus the margins
898+ | x > 5440680 =
899+ (5440680, round $ (5440680 / ((fromIntegral :: Integer -> Double) x)) * (fromIntegral y))
900+ | otherwise = (x, y)