Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

html - BATCH Multiple Image Printing on Single Sheet

I got 1000's of images on a folder & I wanna resize & print 12 to 24 images per sheet.Using BATCH script to collect images from a folder and output an HTML script with 3 colums & 6 rows per page , open in firefox (shrink to fit &print) .

how to add filename, date on every image on html script.

del "c:zz.html"
setlocal EnableDelayedExpansion
set /a "p=1"
set /a "m=2"
set /a "w=4"
echo ^<table cellspacing="5" style="border:1px solid black;"^> >>"c:zz.html"
for /f "delims=" %%i in ('dir /s/b /a-d f:jpegmdl*.jpg') do (
set /a "p=p+1"
echo !p!
if !p! == !m! (
set "bo=")
if !p! == !w! (
set "p=1"
set "bo=<tr>")
echo !bo!^<td^>^<img width=320 height=260 src="file:\%%i"^> >>"c:zz.html"
)
start C:Program FilesMozilla Firefoxfirefox.exe "c:zz.html" &exit

Here is the usable code

del "c:zz.html"
setlocal EnableDelayedExpansion
set /a "p=1"
set /a "m=2"
set /a "w=4"
echo ^<table cellspacing="5" style="border:1px solid black;"^> >>"c:zz.html"
for /f "delims=" %%i in ( 'dir /s/b /a-d %1*.*' ) do (
echo %1
set /a "p=p+1"
echo !p!
if !p! == !m! (
set "bo=")
if !p! == !w! (
set "p=1"
set "bo=<tr>")
echo !bo!^<td^>^<img width=320 height=260 src="file:\%%i"^>^<br^>%%~nxi  >>"c:zz.html"
)
start I:HEVCm-ozm.f -no-remote -profile "I:HEVCm-ozy" "c:zz.html" &exit

This reg key provide Right-Click to print any folder. (change the source folder as you like)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOTDirectoryshell==jam==j]
@="Print===ALL=IN=1=="

[HKEY_CLASSES_ROOTDirectoryshell==jam==jCommand]
@="I:\s\PPP_Print\p.cmd "%1""
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recognize that you put a bit of effort into starting your project, but you appear to need lots of help. I appreciate your sharing your progress thusfar. I guess this makes you worthy of a Christmas present. :)

Your question has several component questions that I see.

1. I wanna resize & print 12 to 24 images per sheet.

Pick one. Do you want 12 or 24? If you want the number to be variable based on the height of the images you're printing, you'd probably be better off investigating Imagemagick as suggested above.

2. Using BATCH script to collect images from a folder and output an HTML script with 3 colums & 6 rows per page

3 * 6 = 18. Let's go with that. An 8.5"x11" page will handle a bit over 900px of height, so set the height of your table cells to 150px. (150 * 6 = 900.)

3. open in firefox (shrink to fit &print)

You can trigger the print dialog by including a bit of JavaScript to call window.print();. You still have to click "Print".

4. how to add filename, date on every image on html script.

Assume %%I is the variable assigned to capture filenames in your for loop. To get the basename.ext of a file, use %%~nxI. To get the file's last modified date and time, use %%~tI. See the last two pages of help for in a console window for more information on this syntax.

Use CSS position: absolute on both your text and your images, and position: relative on the td elements containing them. Either that, or you can load your images as background images and just display the text inline on top. This requires you to configure Firefox to print background images, which might not be configured, though.

To put 18 images on a page, loop through your image files 18 at a time, then generate an HTML table once the 18th image is found. In your CSS add a @media print declaration to ensure each table triggers a page break when printed. To generate your HTML, I suggest that it'd make your code a little more readable and easier to maintain, to employ a batch heredoc function.

Merry Christmas!

@echo off
setlocal enabledelayedexpansion
:: thumbnails.bat
:: generates print layout of *.jpg in current folder
:: https://stackoverflow.com/a/27652107/1683264

set "htmlfile=out.html"

call :heredoc head >"%htmlfile%" && goto end_head
<^!doctype "html">
<html>
    <head>
        <style type="text/css">
            a { text-decoration: none; }
            img {
                max-width: 200px;
                max-height: 150px;
                position: absolute;
                left: 0px;
                bottom: 0px;
            }
            td {
                border: 1px solid black;
                position: relative;
                width: 200px;
                height: 150px;
            }
            span {
                position: absolute;
                left: 5px;
                top: 5px;
                color: purple;
                font-family: "Times New Roman";
                font-size: 11px;
                background: rgba(255, 255, 255, 0.6);
                top: 3px;
                left: 3px;
            }
            @media print {
                table { page-break-after: always; }
            }
        </style>
        <script type="text/javascript">
            addEventListener('load', function() { window.print(); }, false);
        </script>
    </head>
    <body>
:end_head

set count=1
set images=
for %%I in (*.jpg) do (
    set images=!images! "%%~fI"
    if !count! equ 18 (
        call :build_table !images:~1!
        set images=
        set count=0
    )
    set /a count+=1
)

if %count% gtr 1 call :build_table !images:~1!

call :heredoc body >>"%htmlfile%" && goto end_body
    </body>
</html>
:end_body

start "" "firefox" -new-tab "file:///%CD:=/%/%htmlfile%"

:: End of main script
goto :EOF

:build_table <img1> <img2> ... <img18>
setlocal enabledelayedexpansion
set count=1
for %%I in (%*) do (
    set "src=%%~I"
    set "img!count!=<a href="file:///!src:=/!"><img src="file:///!src:=/!" />"
    set "desc!count!=<span>%%~nxI<br />%%~tI</span></a>"
    set /a count+=1
)
call :heredoc table >>"%htmlfile%" && goto end_table
        <table cellspacing="5">
            <tr>
                <td>!img1!!desc1!</td>
                <td>!img2!!desc2!</td>
                <td>!img3!!desc3!</td>
            </tr>
            <tr>
                <td>!img4!!desc4!</td>
                <td>!img5!!desc5!</td>
                <td>!img6!!desc6!</td>
            </tr>
            <tr>
                <td>!img7!!desc7!</td>
                <td>!img8!!desc8!</td>
                <td>!img9!!desc9!</td>
            </tr>
            <tr>
                <td>!img10!!desc10!</td>
                <td>!img11!!desc11!</td>
                <td>!img12!!desc12!</td>
            </tr>
            <tr>
                <td>!img13!!desc13!</td>
                <td>!img14!!desc14!</td>
                <td>!img15!!desc15!</td>
            </tr>
            <tr>
                <td>!img16!!desc16!</td>
                <td>!img17!!desc17!</td>
                <td>!img18!!desc18!</td>
            </tr>
        </table>

:end_table
endlocal
goto :EOF

:: https://stackoverflow.com/a/15032476/1683264
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...