WkHtmlToPdf DLL prototype

yes provided you have access to the file “…/…/logo.jpg” then read it into ST and do more or less as Koen showed above. Maybe simplify it a bit and just replace the reference to logo.jpg with the base64 encoded text. Off the top of my head, something like:

  st       stringTheory
  img      stringTheory
  fname    stringTheory  ! filename of image
  lStart   long,auto
  lEnd     long,auto
  srcStart long,auto
  srcEnd   long,auto
  replaces long,auto
    code
    if ~st.loadFile('myHTML.htm') 
      message('warning: html file not loaded: ' & st.lastError)
      <error>  ! return or exit etc.
    end

    replaces = 0
    lStart = 0
    loop
      lStart += 1
      lEnd = 0
      st.findBetweenPosition('<<img ','/>',lStart,lEnd) 
      if lStart = 0    then break.  ! start/end not found
      if lStart > lEnd then cycle.  ! no contents between start/end
      srcStart = lStart
      srcEnd   = lEnd
      st.findBetweenPosition('src="','"',srcStart,srcEnd,st:NoCase)
      if srcStart = 0 or srcStart > srcEnd then cycle.
      fName.setValue(st.slice(srcStart,srcEnd))
      fName.trim()
      if fName.startsWith('http:') or fName.startsWith('cid:') then cycle.
      if ~img.loadFile(fName.getValue())  ! load the image
        st.trace('warning: file [' & fName.getValue() & '] not loaded: ' & st.lastError)
        cycle
      end
      if img.len() = 0 
        st.trace('warning: zero length file [' & fName.getValue()  & ']')
        cycle
      end      
      img.base64Encode(st:noWrap)
      replaces += st.replace(st.slice(srcStart,srcEnd), |
                  'data:image/' & fName.extensionOnly() & |
                  ';base64,' & img.getValue(),1,srcStart,srcEnd)
    end             
    if replaces then st.saveFile('myUpdatedHTML.htm').

As I say that’s off the top of my head and totally untested but should give you the general idea. Yell out if you have any problems,

cheers

Geoff R

1 Like